3

I am trying this code:

int x[] = {1,5};
int y[] = {3,6};
int *w[] = {x,y};
int **z = w;


printf("%d", **z[1]);
cout<<**z[0];

But it doesn't work. My goal is to access to each element inside the x or y arrays. My question comes from this part of a really OpenCV code:

float h_ranges[] = {0, 180}; 
float s_ranges[] = {0, 256};
const float* ranges[] = {h_ranges, s_ranges};

And like to know how is it possible to read each element inside ranges[]?

EDIT: I tryed:

printf("%d", z[1][1]);
cout<<z[0][0];

And it worked, but it's interesting for me to know why

printf("%d", **z[1][1]);
cout<<**z[0][1];

Doesn't work, but the following code works

printf("%d", *w[1]);
cout<<*w[0];

What is the difference between w and z here?

EDIT2:

Why the result of

printf("%d", *w[1]);
cout<<*w[0];

Is same as the result of:

printf("%d", *z[1]);
cout<<*z[0];
7
  • 3
    Do you know what the type of z[1][1] is? If yes why do you think that adding two "stars" at the front is allowed? Commented Jun 16, 2018 at 10:05
  • @UnholySheep: Thank you! I got it! Commented Jun 16, 2018 at 10:07
  • @UnholySheep: But it is still confusing why *w[0] and *z[0] have same result? In fact I can not imagine truly the structure of z at this point! Commented Jun 16, 2018 at 11:00
  • 1
    **z[0] is dereferencing three times, that's why your cout does not work. Commented Jun 17, 2018 at 10:34
  • 1
    Let's recap: *x is another way of writing x[0], so **z is like z[0][0]. Commented Jun 17, 2018 at 10:37

1 Answer 1

3

Why z[1][1] works?

z is a pointer to pointer to int in w array. Dereferencing *z will give you pointer to the 1st int in {1,5} and dereferincing it once more **z will give you that int, which is 1. This is identical to doing z[0][0] or *(*(z + 0) + 0).

z[1][1] is the same as *(*(z + 1) + 1). Dereferencing *(z + 1) will give you the pointer to the first int in the second array {3,6}. Incrementing *(z + 1) + 1 will give you the pointer to the second int in the second array {3,6}, Dereferencing *(*(z + 1) + 1) will give you the actual second int of the second array {3,6}, which in your case is 6;

Why **z[1][1] doesn't work?

With z[1][1] will get you an int (see above), 6 in your example. So what happens when you try to dereference an int, let alone twice? An error!

Why *w[1] works?

w is array of pointers. With w[1] you get the second pointer from that array which points to the array of 2 ints, {3,6}. If you dereference it, you will get the 1st int of that array, which in your case is 3.

Why the result of *w[1] is same as the result of *z[1]?

Let's visualize it:

{1,5} {3,6}
 |     |
{w[0], w[1]}
 |     | 
 z     z+1

Array w is array of 2 pointers to int, which are w[0] and w[1]. w[1] is the second pointer to int in that array. The int it points to is 3. z is a pointer to pointer to int, or in this case pointer to w[0], which is pointer to 1. z[1] is the same as *(z+1) and it makes z to point to the next pointer before dereferencing result, which in this case is w[1], which points to 3. Dereferencing both w[1] and z[1] yields the same result, because ultimately they both point to the same int, which is 3.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much! But may you explain more about *(*(z + 0) + 0). What is exactly meaning of z+0 or z+1 ? What is the z in this point that you +1 it? I think z is an address and you step to next address by +1.
I also added a new question (EDIT 2) to my previous questions, that is vague for me!
@Hasani I have added a little visualisation. I'm afraid if you have any more questions after that you will have to post a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.