unusual return

Feb 17, 2011 at 2:26am
Hi,

I am trying to understand how a char * const can return into an int.

specifically, when you use the strstr function if you are trying to return a specific integer for where a sub string starts to occur.
Feb 17, 2011 at 3:08am
It seems you want to know the offset. Consider:

1
2
3
char s[] = "Hello, world!";
char sub[] = "world";
const char* p = strstr(s, sub);

You want to know the value of i in s[i] == 'w', right? That value is simply the (pointer) arithmetic difference between a pointer to s[i] (that is, the address of s[i]) and a pointer to s[0]. Since you have both, you can say that s[p - s] == 'w'.

You might want to guarantee that p is not NULL (or that the substring will be found) before you write something like that, though, otherwise you'll get an out of bounds access.
Last edited on Feb 17, 2011 at 3:17am
Feb 17, 2011 at 3:31am
Thanks!

I figured the concept out. My problem is that I have a function with a return value that has to be an integer, I was concerned about an error since s, sub and p are all char values and not integers. ( the notorious MyString class...)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//assume this is a function of MyClass for illustration purposes
int MyClass::Find(const s1 & substr)
{
const char* p = strstr(s1, substr);
return p -s1;            //this is the important part
}
//assume we are switching to main() for illustration purposes
char  s[] = "Hello, world!";
char sub[] = "world";
int myIndex; 

myIndex = MyClass finder.Find(s,sub);  //I am hoping this will return a value of 7



would my index indeed be an int even though s and sub are chars?

I am not asking how to make it work, I am asking will it return an integer or will I have to go another route to do so.

also if it does return an integer why, what is the exception rule?

I hope that was a bit clearer. On a brighter note, that was a stellar example you gave.
It allowed me to alter it without giving you my homework, so I could still do that myself.
Last edited on Feb 17, 2011 at 3:32am
Feb 17, 2011 at 3:45am
You see, s, sub and p in your code (the method parameter list is screwed up) are not chars. They're of type char* (const char* in p's case, but let's abstract that away for now). They're all pointers. Pointers hold addresses, and addresses are integral numbers.

And technically, p - s is of type ptrdiff_t, so that's what your function should return.

On a brighter note, that was a stellar example you gave.
It allowed me to alter it without giving you my homework, so I could still do that myself.

Thanks, I'm glad it was helpful.
Last edited on Feb 17, 2011 at 3:47am
Feb 17, 2011 at 3:58am
just looked up ptrdiff_t "This is a signed integral type, and as such can be casted to compatible fundamental data types." As lame as it is for me to say it: wahoo!

Thanks for all the help there, and the good explanation. I appreciate it filipe, hopefully one day I will be good enough to give advice with as much integral knowledge of the language.

Dan Schwartz

Last edited on Feb 17, 2011 at 4:00am
Topic archived. No new replies allowed.