When I cout in the example above, why dont they have the same memory address?
In a loop, where I increment to the next value, when do I stop if I only have a pointer to the first and last char. ( And not check for null or using int length, just first and last pointer)
I thought I could compare the addresses, but they are not the same.
haystack is a pointer. &haystack is the place in memory where that pointer is. That value - the location in memory where the pointer is - has NOTHING to do with what it's pointing at. Nothing at all to do with "hey".
But I don't think you meant that. I think you want the place in memory where the zero after "hey" is, right? That's not what you're outputting. You're output the place in memory where the pointer named haystack is, but what you want is the place in memory where the end of the string is.
So basically, the line below means: print out every char starting from the address of d[3] to null?
cout << &d[3];
and this, just the address of d[3]
cout << (void*) &d[3];
Strange. You expect that if you store "the address of", and then print out "the address of" that it would return "the address of". And not the entire string FROM "the address of".
The language would have been more logical if they had maintained the red thread, and used a different character (with a different meaning) if you wanted the entire string instead from that address. Now it just gets confusing from time to time
heykiln
h
0x79b7f609e138
kiln
0x79b7f609e150
-----------------
kiln
k
0x79b7f609e130
n
0x79b7f609e148
-----------------
0x79b7f609e130
Exit code: 0 (normal program termination)
If we're giving the function "the address of", and the function stores it in "haystack_end". Then "haystack_end" should be "the address of" without "&" because its already provided.
I know it's not working that way, but that would be more logical I think.
1. The address of (&) a variable is of type pointer - char* in this case
2. C-strings are delimited (terminated) by a hidden '\0'
3. So &d[3] is the address of the 4th character in the C-style array 'd'
4. &d[3] is of type char* - it is the addrees in memory of the 4th character - the type is exactly as your function header specifies
5. Given the way C-strings work the cout prints from char[3] to the '\0' terminator.
6. If you removed the & from your function call then the types are incompatible and your program won't run. Try it.
In function 'int main()':
28:22: error: invalid conversion from 'char' to 'char*' [-fpermissive]
5:6: note: initializing argument 2 of 'void str_search(char*, char*)'
PS Fiddly more than illogical - but in the long run pretty powerful.
Strange. You expect that if you store "the address of", and then print out "the address of" that it would return "the address of". And not the entire string FROM "the address of".
operator << simply behaves differently for char* in this case. Other (supported) pointers, you just get the address. Pass it a char* , and it outputs that char and all following until it hits a zero. It's a convenience because 99 times out of a hundred, that's the behaviour people want when thay have a char pointer and they want to feed it to the output.