简体   繁体   中英

ELI5: What is the data type of `int *p[]`

I don't understand what the datatype of this is. If its a pointer or an array. Please explain in simple terms. To quote what was in the book-

If you want to pass an array of pointers into a function, you can use the same method that you use to pass other arrays—simply call the function with the array name without any indexes. For example, a function that can receive array x looks like this:

void display_array(int *q[])
{
  int t;
  for(t=0; t<10; t++)
    printf("%d ", *q[t]);
}

Remember, q is not a pointer to integers, but rather a pointer to an array of pointers to integers. Therefore you need to declare the parameter q as an array of integer pointers, as just shown. You cannot declare q simply as an integer pointer because that is not what it is.

cite: C++: The Complete Reference, 4th Edition by Herbert Schildt, Page 122-123

... pointer to an array of pointers to integers

No it's not. q is the type int *[] . Which is an invalid (or possibly incomplete, depending on context) type in C++, and only valid in some places in C. Arrays must have a size.

The type int *[] is an (unsized) array of pointers to int . It is itself not a pointer.


The confusion probably comes from the fact that an array can decay to a pointer to its first element.

For example, lets say we have this array:

int a[20];

When plain a is used, it decays to a pointer to its first element: a is equal to &a[0] .

This is how it's built up:

  • int is the type "int".
  • int* is the type "pointer to int"
  • int* [] is the type "array (of unknown bound/length) of pointer to int"
  • int* p[] is the declaration of a variable or parameter named p of the type above.
   int *p[]
//      ^

p is

   int *p[]
//       ^^

p is an array of unspecified size (possibly illegal, depends on context)

   int *p[]
// ^^^^^

p is an array of unspecified size of pointers to int

Meaning each element of p is a pointer:

int foobar = 42;
p[0] = NULL;
p[1] = &foobar;

I don't understand what the datatype of this is

If it's any comfort, neither does the author of the book you are reading.

Remember, q is not a pointer to integers, but rather a pointer to an array of pointers to integers.

This is bullschildt .

  • Before adjustment of parameters, q is an array of pointers to integers.
  • After adjustment of parameters, q is a pointer to the first element of an array of pointers to integers. Equivalent to int** q , a pointer to pointer to an int .

Nowhere is it "a pointer to an array of pointers to integers". That would have been int* (*q)[] .

I would advise to stop reading that book.

The key here is that any array that is part of a parameter list of a function, gets adjusted ("decays") into a pointer to the first element. So it doesn't matter if you type int* q[666] or int* q[] , either will be silently replaced by the compiler with int** "behind the lines".

This is actually the reason why we can write [] in a parameter list - normally an empty array would be an incomplete type that can't be used before completion elsewhere. But since parameters always get adjusted, they are never of array type, and it doesn't matter that the original type was incomplete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM