0

Possible Duplicate:
Is array name a pointer in C?

If I define:

int tab[4];

tab is a pointer, because if I display tab:

printf("%d", tab);

the code above will display the address to the first element in memory.

That's why i was wondering why we don't define an array like the following:

int *tab[4];

as tab is a pointer.

Thank you for any help!

Community
  • 1
  • 1
Matthew
  • 10,988
  • 11
  • 54
  • 69
  • 1
    None of those is a pointer. – cnicutar Nov 25 '12 at 14:54
  • 1
    "the code above will display the address to the first element in memory." - or it won't. Printing a pointer using `%d` is undefined behavior. You should have written `printf("%p", (void *)tab)`. –  Nov 25 '12 at 14:58

5 Answers5

6

tab is a pointer

No, tab is an array. An int[4] to be specific. But when you pass it as an argument to a function (and in many other contexts) the array is converted to a pointer to its first element. You can see the difference between arrays and pointers for example when you call sizeof array vs. sizeof pointer, when you try to assign to an array (that won't compile), and more.

int *tab[4];

declares an array of four pointers to int. I don't see how that is related to the confusion between arrays and pointers.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • This was the confusion: " But when you pass it as an argument to a function (and in many other contexts) the array is converted to a pointer to its first element." – Matthew Nov 25 '12 at 15:00
2

tab is not a pointer it's an array of 4 integers when passed to a function it decays into a pointer to the first element:

int tab[4];

And this is another array but it holds 4 integer pointers:

int *tab[4];

Finally, for the sake of completeness, this is a pointer to an array of 4 integers, if you dereference this you get an array of 4 integers:

int (*tab)[4];
iabdalkader
  • 17,009
  • 4
  • 47
  • 74
  • This sort've contradicts what Daniel Fischer says above where you say "Finally, for the sake of completeness, this is a pointer to an array of 4 integers, if you dereference this you get an array of 4 integers:" – Jerry Saravia Sep 26 '13 at 19:07
  • @JerrySaravia no it doesn't, the parentheses override the precedence – iabdalkader Sep 26 '13 at 19:26
  • you're right. I interpreted it as being the example code first and then your explanation rather than your explanation then the code. Thanks for the clarification. – Jerry Saravia Sep 26 '13 at 19:55
1

You are not completely wrong, meaning that your statement is wrong but you are not that far from the truth.

Arrays and pointers under C share the same arithmetic but the main difference is that arrays are containers and pointers are just like any other atomic variable and their purpose is to store a memory address and provide informations about the type of the pointed value.

I suggest to read something about pointer arithmetic


Considering the Steve Jessop comment I would like to add a snippet that can introduce you to the simple and effective world of the pointer arithmetic:

#include <stdio.h>

int main()
{
  int arr[10] = {10,11,12,13,14,15,16,17,18,19};
  int pos = 3;

  printf("Arithmetic part 1 %d\n",arr[pos]);

  printf("Arithmetic part 2 %d\n",pos[arr]);

  return(0);
}

arrays can behave like pointers, even look like pointers in your case, you can apply the same exact kind of arithmetic by they are not pointers.

Community
  • 1
  • 1
user1849534
  • 2,329
  • 4
  • 18
  • 20
  • 1
    In fact there isn't any such thing as "array arithmetic". The reason they share the same arithmetic is that array-to-pointer decay occurs *before* any arithmetic in expressions like `tab + 1`, `tab[1]` or (absurdly) `1[tab]`. – Steve Jessop Nov 25 '12 at 15:09
0
int *tab[4];

this deffinition means that the tab array contains pointers of int and not int

From C standard

Coding Guidelines

The implicit conversion of array objects to a pointer to their first element is a great inconvenience in trying to formulate stronger type checking for arrays in C. Inexperienced, in the C language, developers sometimes equate arrays and a pointers much more closely than permitted by this requirement (which applies to uses in expressions, not declarations). For instance, in:

file_1.c

extern int *a;

file_2.c

extern int a[10];

the two declarations of a are sometimes incorrectly assumed by developers to be compatible. It is difficult to see what guideline recommendation would overcome incorrect developer assumptions (or poor training). If the guideline recommendation specifying a single point of declaration is followed, this problem will not 419.1 identifier declared in one file occur. Unlike the function designator usage, developers are familiar with the fact that objects having an array function designator converted to typetype are implicitly converted to a pointer to their first element. Whether applying a unary & operator to an operand having an array type provides readers with a helpful visual cue or causes them to wonder about the intent of the author (“what is that redundant operator doing there?”) is not known.

Example

static double a[5];

void f(double b[5])
{
    double (*p)[5] = &a;
    double **q = &b; /* This looks suspicious, */

    p = &b; /* and so does this. */
    q = &a;
}

If the array object has register storage class, the behavior is undefined

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
0

Under most circumstances, an expression of array type will be converted ("decay") to an expression of pointer type, and the value of the expression will be the address of the first element in the array. The exceptions to this rule are when the array expression is an operand of the sizeof, _Alignof, or unary & operators, or is a string literal being used to initialize another array in a declaration.

int tab[4];

defines tab as a 4-element array if int. In the statement

printf("%d", tab); // which *should* be printf("%p", (void*) tab);

the expression tab is converted from type "4-element array of int" to "pointer to int".

John Bode
  • 119,563
  • 19
  • 122
  • 198