2

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?

asked Nov 14, 2012 at 17:01

3 Answers 3

5

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.

Bernd Elkemann
23.6k4 gold badges40 silver badges72 bronze badges
answered Nov 14, 2012 at 17:14
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered Nov 14, 2012 at 17:21

Comments

-1

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 ainitialization is not in correct manner

Since you have used () so comma operator will be in effect and will initialize with 3and 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.

answered Nov 14, 2012 at 17:02

6 Comments

what about assignment to &a[0]. @Omkant
@MohitSehgal: I think you got it
the address of the first element of a is &a[0]
@newacct: then what is this int *ptr = &a[0][0] ? can you please elaborate
@Omkant: that is the address of the first element of the first element of a
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.