Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of the following?

Tags:

arrays

c

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

I understand that the sampleArray now points to the first element of the array.

However, what does it mean when I say &sampleArray? Does it mean I am getting the address of the sampleArray variable? Or does it mean a two-dimensional array variable?

So, can I do this:

int (*p)[5] = &sampleArray?
like image 947
vj01 Avatar asked Dec 03 '22 13:12

vj01


1 Answers

No, sampleArray does not really point to the first element of the array. sampleArray is the array.

The confusion arises because in most places where you use sampleArray, it will be replaced with a pointer to the first element of the array. "Most places" means "anywhere that it isn't the operand of the sizeof or unary-& operators".

Since sampleArray is the array itself, and being the operand of unary-& is one of the places where it maintains that personality, this means that &sampleArray is a pointer to the whole array.

like image 173
caf Avatar answered Dec 24 '22 08:12

caf