Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is int *array[32] a pointer to an array of 32 ints, or an array of 32 pointers to int? Does it matter?

If I write

int *columns[32];

am I defining an array with 32 pointers to ints?
Or is it a pointer to an array of 32 ints?

How do I differentiate between the two? Is there a difference?

like image 259
Carson Myers Avatar asked Sep 03 '09 07:09

Carson Myers


People also ask

What is the difference between pointer to integer and array?

UPDATE: You can notice that pointer to integer incremented by 4 bytes (size of 32 bit integer) whereas pointer to array of integer incremented by 20 bytes (size of int arr [5] i.e. size of 5 int of 32 bit each). This demonstrates the difference. Show activity on this post.

What is the difference between int* and int (*x) [6]?

The difference is that int* points to an int type, but int (*x) [6] points to an array of 6 ints. Actually in your example, is undefined** behavior, you know these are of two different types, but in C you do what you want. I'll just use a pointer to an array of six ints. Would be printed.

How do you declare a pointer to an array in C?

Declaration of the pointer to an array: // pointer to an array of five numbers int (* ptr) [5] = NULL; The above declaration is the pointer to an array of five integers. We use parenthesis to pronounce pointer to an array.

What happens when you dereference an array in C++?

On dereferencing a pointer expression we get a value pointed to by that pointer expression. Pointer to an array points to an array, so on dereferencing it, we should get the array, and the name of array denotes the base address. So whenever a pointer to an array is dereferenced, we get the base address of the array to which it points.


2 Answers

Expanding on a comment to another answer:

There's a fairly straightforward procedure for reading C declarations. Start with the leftmost identifier in the declarator and work your way out, remembering that [] and () bind before *. Given the declaration

int *columns[32];

break it down as

     columns                  -- columns
     columns[32]              -- is a 32-element array
    *columns[32]              -- of pointers
int *columns[32]              -- to int.  

If the declaration had been

int (*columns)[32];

then it would break down as

      columns                 -- columns
    (*columns)                -- is a pointer
    (*columns)[32]            -- to a 32-element array
int (*columns)[32]            -- of int.  

This will also help you build up complex declarations. Suppose you wanted to declare an array of pointers to functions returning pointers to arrays of char:

          f                     -- f
          f[N]                  -- is an N-element array
         *f[N]                  -- of pointers
        (*f[N])()               -- to functions
       *(*f[N])()               -- returning pointers
      (*(*f[N])())[M]           -- to M-element arrays
     *(*(*f[N])())[M]           -- of pointers
char *(*(*f[N])())[M];          -- to char

cdecl is a nice tool, but after you'd done this exercise a few times, you shouldn't need it.

like image 108
John Bode Avatar answered Oct 10 '22 20:10

John Bode


When in doubt - ask cdecl

$> cdecl 
Type `help' or `?' for help
cdecl> explain int *columns[32]
declare columns as array 32 of pointer to int

EDIT In response to comments: I found cdecl source on Google Code Search. It requires GNU readline library. I think it shouldn't be a problem to compile it on Mac OS X or Windows.

like image 27
qrdl Avatar answered Oct 10 '22 20:10

qrdl