how pointers in array of pointers can access to that array

declare
 
int *p[3];


that gives an array with 3 pointers, but how 3 pointers can have a link in the memory?
Last edited on
What do you mean by "have a link"?
i mean have a relation or relationship
What relationship? Three pointers is three pointers.
You could have:
1
2
3
int * a;
int * b;
int * c;

However, just like with other datatypes, it is often more convenient to refer to group of objects with one name (of array), and you can iterate over array elements, unlike over various names.

Which looks more convenient:
1
2
3
4
5
6
7
8
9
10
11
// A
for ( auto x : arr ) {
  cout << *x << '\n';
}

// B
cout << *a << '\n';
cout << *b << '\n';
cout << *c << '\n';
cout << *d << '\n';
cout << *e << '\n';
If I understand the question properly, you have to assign values to the pointers.


1
2
3
4
5
6
7
8
int *p[3];
int a = 3;
int b = 5;
int c = 6;

p[0] = &a;
p[1] = &b;
p[2] = &c;
No, i want to discuss about 3 pointer of an array
On the memory, It's saved without any orders. But because they are 3 pointer of an array. So I want to know how their relationship show
Sorry for my poor English
@ doug4: i agree with you. but that's not all I want to know
if they are not 3 pointer of an array, it mean there no any relation between them. What is the difference between 3 pointer of an array and specific 3 pointers
What is the difference between three separate double variables and an array of three double elements?

No real difference.

Note: We know that elements of an array are consecutive within one block of memory. Compiler can allocate memory for separate variables as it wish, i.e. from non-consecutive locations.

A pointer is just a variable that stores a number. It is no different from other variable types, except that it has a dereference operator.
I don't really know what you are asking. Do three integers in an array (int i[3]) have a relationship? Three pointers in an array would have the same relationship.

The relationship in an array means you can access the values with a common name and an index. That goes for arrays of ints, arrays of pointers, or arrays of large objects.
The fact that you have declared an array of three pointers means those pointers are in consecutive locations in memory. You can access any of the three pointers using an index. That is the only relationship.

In your example, the three pointers are uninitialized, so they don't point to anything meaningful. There is no relationship there.
Topic archived. No new replies allowed.