0

I understand that ptr is a pointer to the address of M[0].

Therefore, I thought *ptr would represent the value of M[0].

But why do the two show the same value?

Here's the code:

#include <iostream>

using namespace std;

int main(void) {
    int M[3][3]={{1,2,3},{4,5,6},{7,8,9}};
    int (*ptr)[3]; int *p; int **pt;
    ptr = M;
    cout << ptr << " " << *ptr;

    return 0;
}

I look forward to a clear answer to this.

Also, I wonder how I can print out ptr[0].

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 3
    See [What is array to pointer decay?](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay) – Jason Apr 03 '23 at 13:45
  • 3
    `ptr` is not an array of pointers. Instead `ptr` is a pointer to an array of type `int [3]` – Jason Apr 03 '23 at 13:46
  • 1
    To elaborate on Jasons's comment `int (*ptr)[3];` is a pointer to an array. An array of pointers would be `int* ptr[3];` but obviously then `ptr = M;` would not compile. (Try it an see what the error is). – john Apr 03 '23 at 13:49
  • Also `*ptr` is exactly the same as `ptr[0]` **by definition**. So the answer to the question how so I print out `ptr[0]` is that you already are. – john Apr 03 '23 at 13:51
  • Yes, the value of `ptr` is the address of `ptr[0]`, and `*ptr` has the value of `ptr[0]`. But their *types* are different: `int (*)[3]` and `int *` respectively. Your code is converting both those pointers to `void *` (to call the stream's `operator<<()` overload that accepts a `void *` argument). `(void *)ptr` is the address of the first byte in the array `{1,2,3}`, and `(void *)(*ptr)` is the address of the first byte in the `1` - which is the first element of the array `{1,2,3}`. So the address of the same byte is being printed both times. – Peter Apr 03 '23 at 13:51
  • @Peter Are you sure? `*ptr` and `ptr[0]` are identical in type and value for any pointer. – john Apr 03 '23 at 13:53
  • @john I didn't say what you're thinking. The OP is asking why `ptr` and `*ptr`, when printed, both result in the same output (i.e. the pointers have the same printed value). – Peter Apr 03 '23 at 13:55
  • @Peter OK got it, I mistook what *their* was referencing in your second sentence. – john Apr 03 '23 at 14:00

1 Answers1

1

Why do ptr and *ptr show the same value when ptr is an array of pointers?

Your interpretation of ptr is wrong. In particular, ptr is not an array of pointers, but is a pointer to an array of type int[3].

This means that the type of ptr is int (*)[3], which in turn means that the type of *ptr is int[3]. Now, due to a property called array to pointer decay, this resulting array decays to int* (meaning a pointer to its first element) and gives you the output you observed.

More importantly, note that ptr points to the first inner array {1,2,3} which has the same value as *ptr. That is, the type of ptr and *ptr are different but their value is same for a given array.


I wonder how I can print out ptr[0].

From the above discussion, we can see that *ptr is the same as ptr[0], so you're already getting the value you want.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Jason
  • 36,170
  • 5
  • 26
  • 60