简体   繁体   中英

Array Declaration and pointer assignment in C

I am confused in the basics of pointer and array declaration in C. I want to know the difference between following two statements except that base address to array is assigned to ptr in seconed statement.

int a[2][3]= { (1,2,3),(4,5,6)};
int (*ptr)[3] = &a[0];

Please quote examples to clarify. What effect do [3] on R side of line 2 has?

1. Bidimensional array:

int a[2][3]= { {1,2,3},{4,5,6}};

With this statement in memory you have 2x3 integers, all adjacent in memory.I suppose that you know how to access them, but in the case you don't I'll clarify it:

a[0][0] : 1
a[0][1] : 2
a[0][2] : 3
a[1][0] : 4
a[1][1] : 5
a[1][2] : 6

2. Pointer to array:

int (*ptr)[3] = &a[0];

ptr points to a int[3] block of memory.So you can assign it only to an int[3] type:

ptr= &a[0];
ptr= &a[1];

The difference is that this pointer does not have it's own memory, and you have to assign it to an int[3] variable or allocate it:

ptr= malloc (2*sizeof(int[3]);

This way you can use the memory pointed by ptr, if you initialize ptr this way:

for(int j=0; j<2; j++)
    for(int i=0; i<3;i++)
        ptr[j][i]=i+j*3+1;

This case you'll have the same memory representation of int a[2][3], except that this memory is in the heap and not in the stack.You can always choose to realloc/free the memory and this memory is not deleted once your function terminates.

You should know operator precedence rules in C: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedencehttp://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence

int (*ptr)[3] as opposed to int * ptr [3]

The first one is a pointer (notice * is closer to the var name) to arrays of int of size 3 The second one is equal to int (*(ptr [3])) which is an array of size 3 on int pointers.

You can also use this site: http://cdecl.org/ if you have doubts on how to interpret an expression.

a is 2-D array of row size 2 and column size 3.

whereas ptr is pointer to an array of int having size 3

But your array a initialization is not in correct manner

Since you have used () so comma operator will be in effect and will initialize with 3 and 6 and other elements of array a will be 0 to do actual initialization use {}

&a[0] is address of first element of array (means it will print same value for a also).

but this &a[0] comes into effect when you perform pointer arithmetic operation on ptr

In case of array there are 3 things to keep in mind :

---> int *ptr=&a[0][0]; Address of first element ptr++ will give you next element a[0][1]

---> &a[0] = address of first element but if you do int (*ptr)[3]=&a[0] then by doing ptr++ pointer will be increased by the size of whole row and ptr will point to next row directly means now ptr will point to &a[1]

---> &a = address of whole array and if you keep it in int (*ptr)[2][3] =&a and do ptr++ pointer will be increased by the size of whole array.

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