Stop at the last char in array

Hello.

void str_search(char * haystack, char * haystack_end) {

cout << &haystack + 3 << "\r\n";
cout << &haystack_end;
}


int main() {

char d[] = "hey";
str_search(d,&d[3]);

}

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.


Best regards
Volang
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using std::cout;

void str_search(char * haystack, char * haystack_end)
{
  cout << (void *) haystack << "\r\n";
  cout << (void *) (haystack + 3) << "\r\n";
  cout << (void *) haystack_end << "\r\n";
}

int main()
 {
  char d[] = "hey";
  str_search(d,&d[3]);
}

Last edited on
Good explanation Repeater. This works good :) Thank you
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
Last edited on
Run this expanded version and it might throw some additional light on it for you.
(A C-style string always has a '\0' appended at the end.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

using namespace std;

void str_search(char* haystack, char* haystack_end) {
    
    cout <<      haystack << '\n';
    cout <<     *haystack << '\n';
    cout <<     &haystack << '\n';
    cout <<  haystack + 3 << '\n';
    cout << &haystack + 3 << '\n';
    cout << "-----------------\n";
    
    cout <<      haystack_end << '\n';
    cout <<     *haystack_end << '\n';
    cout <<     &haystack_end << '\n';
    cout <<  haystack_end + 3 << '\n';
    cout << &haystack_end + 3 << '\n';
    cout << "-----------------\n";

    cout << &haystack_end << '\n';
}


int main() {
    
    char d[] = "heykiln"; // pick a string longer than 3 characters
    str_search(d, &d[3]);
    
}



heykiln
h
0x79b7f609e138
kiln
0x79b7f609e150
-----------------
kiln
k
0x79b7f609e130
n
0x79b7f609e148
-----------------
0x79b7f609e130
 
Exit code: 0 (normal program termination)
Last edited on
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.
Last edited on
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.
I noticed. So basically "<<" takes the address, and then does its own fancy/extra stuff when the type is char* ?

What if a want to compare two pointers?

char val[] = "light up the world";
char * last = &val[strlen(val)];


if I check: &val[strlen(val)] == last, does it just compare the pointers (address1 with address2) or does it do some massive string comparision?
does it do some massive string comparision?


No. == applied to two char* just compares the memory addresses.
Thanks buddy
Registered users can post here. Sign in or register to post.