Pointers

Hi
I have this code in main:

string input[5];
string* ptr[5];

for(int i = 0; i != 5; ++i)
ptr[i] = &input[i];

readLines(ptr, 5);

The function readLines(string *t[], int nmb) shall read some lines (nmb) too the input table via the pointer table. How is this possible?
A pointer is just an address in memory.

string* ptr[5] declares an array of 5 pointers, and instructs the compiler that they are pointer to strings.

&input[x] is the address of input[x]

So
1
2
for(int x = 0; x != 5; ++x)
   ptr[x] = &input[x];

sets ptr[0] to the address of input[0], etc.

Thus when you call readLines(ptr,5) the function reads the lines to the memory pointed at by ptr, which is the same memory addresses as read using input.
Topic archived. No new replies allowed.