Cindy
Cindy

Reputation: 567

Pointer variable pointing to a one dimensional array or two dimensional array?

I have the following code for a one dimensional array:

#include <stdio.h>
int main()
{
    static int b[4] = {11, 12, 13, 14};
    int (*p)[4];

    p = b;
    printf("%d \n", *(p + 1));

    return 0;
}

Even though I consider "b (the array name)" as a pointer pointing to a one dimensional array, I got a compiling error as

'=': cannot convert from 'int [4]' to 'int (*)[4]'

However, if I change b array into a two dimensional array "a (the array name)", everything works fine. Does this mean that, in the usage of "int (*p)[4];", "*p" has to represent a[] as in the following:

static int a[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };
int (*p)[4];
p = a;

As a result, "int (*p)[4]" only provides the flexibility on the number of rows of a two dimensional array.

Any insights on this problem?

Upvotes: 2

Views: 615

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 310970

If you have an object of some type T like

T a;

then declaration of a pointer to the object will look like

T *p = &a;

Your array b has the type int[4]. So a pointer to the array will look like

int ( *p )[4] = &b;

To output the second element of the array using the pointer you should write

printf("%d \n", *( *p + 1 ) );

Thus your compiler issued the error message

cannot convert from 'int [4]' to 'int (*)[4]

because instead of writing at least

int ( *p )[4] = &b;

you wrote

int ( *p )[4] = b;

On the other hand, an array designator used in expressions with rare exceptions is implicitly converted to pointer to its first element. For example in this declaration

int *p = b;

the array b used as an initializer is converted to pointer to its firs element. The above declaration is equivalent to

int *p = &b[0];

or that is the same

int *p = b + 0;

Using this pointer you can call the function printf like

printf("%d \n", *(p + 1));

If you have a two-dimensional array as

int a[3][4];

then used in expressions it is converted to pointer to its first element that has the type int[4]. So you may write

int ( *p )[4] = a;

If you want to declare a pointer to the whole array as a single object you can write

int ( *p )[3][4] = &a;

Upvotes: 0

user16719198
user16719198

Reputation:

a pointer pointing to a one dimensional array,

No, it points directly to the first element. Likewise:

int *p = b;

is enough.

The number 4 is not really part of any type here;

 static int b[] = {11, 12, 13, 14};

It can be left out in the declaration. (Because it is the first dimension unless you make it 2D)


This (from AA)

int (*p)[4] = &b;
...
printf("%d \n", *( *p + 1 ) );

is just a obfuscated and overtyped version of:

int (*p)[] = &b;
...
printf("%d \n", (*p)[1] );

This replaces b with (*p), normally not what you want.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409176

Arrays naturally decay to pointers to their first elements, depending on context. That is, when such a decay happen then plain b is the same as &b[0], which have the type int *. Since the types of p and b (or &b[0]) are different you get an error.

As for a it's the same thing here, it decays to a pointer to its first element, i.e. a is the same as &a[0]. But since a[0] is an array of 4 elements, then &a[0] is a pointer to an array of four elements, or int (*)[4]. Which is also the type of p in the second example.

Upvotes: 1

Related Questions