0

Possible Duplicate:
Is array name a pointer in C?

char arr[1024];
arr++ //move arr to arr+1, error!

I have heard the the name of a char array is a char pointer, is it?

Community
  • 1
  • 1
lovespring
  • 18,247
  • 41
  • 98
  • 148

5 Answers5

4

The name of an array decays to an pointer to its first element sometimes.
An expression with array type will convert to a pointer anytime an array type is not legal, but a pointer type is.

You cannot do:

arr++;

Because array is an non modifiable l-value.

Alok Save
  • 196,531
  • 48
  • 417
  • 525
  • sometimes, we can use array name as a pointer (e.g. if a function need a pointer), is this a decay? when we can decay? is there a rule? – lovespring Nov 12 '11 at 08:20
  • @lovespring: I stated the rule in my answer. *An expression with array type (which could be an array name) will convert to a pointer anytime an array type is not legal, but a pointer type is.* Example is: Declaration of an array as a function parameter is converted into a declaration of a pointer. – Alok Save Nov 12 '11 at 08:22
3

An array is a block of memory to which you gave a name.

What does it mean to "increment" it by one? That doesn't make any sense.

A pointer is a memory address. "Incrementing" it by one means to make it point to the element after it.

Hope that makes sense.

user541686
  • 197,378
  • 118
  • 507
  • 856
1

Arrays and pointers are not always exchangeable. A more interesting example to illustrate the difference between arrays and pointers is a 2D array:

Consider int **a and int b[3][3].

In the first approach to a 2D array we have a 1D array of pointers to 1D arrays in memory (of course you have to allocate the memory dynamically to use this).

In the second approach of actually using a 2D C array, we have the elements laid out sequentially in memory, and there's no separate location where an array of pointers are stored.

If you try to dereference b, you get a pointer to its first element (i.e. b gets converted to an int (*)[3] type).

Szabolcs
  • 23,023
  • 7
  • 79
  • 164
0

nothing, apart from looks..

Well and as you defined the array there you declared already 1024 bytes for the array. Also you obviously can't change the array "base".

paul23
  • 7,894
  • 12
  • 54
  • 134
0

An array name is for most (but not all) purposes identical to a constant pointer (not to be confused with a pointer to a constant). Because it's a constant, you cannot modify it with the increment operator ++. There's a good answer explaining this in more detail in an older similar question.

Community
  • 1
  • 1
ibid
  • 3,741
  • 20
  • 16