tags:

views:

216

answers:

6
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?
A: 

&sampleArray is effectively the address of the sampleArray variable.

int (*p)[5] = &sampleArray is an proper assignment for &sampleArray

The variable p is pointer to five integers and &sampleArray is the address (i.e. a pointer) of an array of integers: that works!

The parenthesis surrounding p, which I missed on first take, are very important because they force the use of * to be for p, i.e. "p is a pointer to" is the beginning of p's decription.

When I find myself in doubt with c declarations I refer to the right-left rule (or also to K&R book ;-) )

mjv
vj01
No, the variable `p` is **not** an array of pointers to integer. It is a pointer to an array of 5 integers, and the expression is well-formed.
caf
@cag: you are quite right I missed the parenthesis...
mjv
+7  A: 

The name of an array evaluates to its address (which is the address of its first element), so sampleArray and &sampleArray have the same value.

They do not, however, have the same type:

  • sampleArray has a type of int* (that is, a pointer-to-int)
  • &sampleArray has a type of int (*)[5] (that is, a pointer to an array of five ints).

int (*p)[5] declares a pointer p to an array of five ints. Ergo,

int (*p)[5] = &sampleArray; // well-formed :)
int (*p)[5] = sampleArray;  // not well-formed :(
James McNellis
WhirlWind
@WhirlWind actually if you try it (not sure if it's guaranteed) they have the same value. they are of different types though.
cobbal
@cobbal interesting enough, you're right...
WhirlWind
SysAdmin
Minor nit: the type of the expression `sampleArray` is `int [5]`, which *in most contexts* will be converted implicitly (decay) to `int *`.
John Bode
+8  A: 

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.

caf
A: 

Some sample code:

#include <stdio.h>

int main()
{
  int a[] = {1,2,3,4,5};
  int (*p)[5] = &a; 
  //Note
  //int (*p)[5] = a; // builds (with warnings) and in this case appears to give the same result

  printf( "a = 0x%x\n" , (int *)a);
  printf( "&a = 0x%x\n", (int *)(&a));
  printf( "p = 0x%x", (int *)p);

  return;
}

The output - same for both ways.

$ ./a.exe 
a = 0x22cd10
&a = 0x22cd10
p = 0x22cd10

Compiler

$ gcc --version gcc (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125) Copyright (C) 2004 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Tanj
A: 

that is called insilization of an array. array is collection of similar type of data like int,char,float number like that.