1

I'm in the process of learning C, and I'm currently on the pointer section: http://www.learn-c.org/en/Pointers

At one point in the guide it says:

And since arrays are actually pointers...

How are arrays actually pointers? I know if I create char array[20], array is the memory address of the first element of the array, (does it "point" to that, or just hold the value), right? Is that what they mean?

As I understand it an array is just a sequence of memory addresses, and the subscript access [0], [1], etc. are just the values at those memory addresses. Am I totally off?

4 Answers4

3

You don't have to trust everything that you find on the internet: arrays are not pointers. There are contexts in which arrays and pointers are treated in the same way, but for the most part arrays are, well, arrays, not pointers.

The most important aspects that make arrays differ from pointers are that

  • Arrays have a specific area for their data; pointers need to be associated with a storage area before use
  • Arrays cannot be re-assigned to refer to a different place in memory
  • Arrays have a specific size that can be retrieved through sizeof
  • Array data will be copied if an array member is included in a struct

One context in which arrays and pointers behave the same is when you pass them to a function: in this situation the compiler takes the address of the array, makes a pointer from it, and passes that pointer to a function. The process is sometimes referred to as "arrays decaying to a pointer".

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

It means that the author does not understand C arrays and pointers, and should not be writing guides that attempt to describe the C language.

Arrays and pointers have a close relationship, but they are distinct things. Arrays are not pointers. An array object consists of an ordered sequence of objects of some element type; a pointer object contains a single memory address.

Array manipulation is generally done via pointers to the element type, and there are several language rules designed to make this convenient (unfortunately, those rules also cause a great deal of confusion).

The most notable such rule is that an expression of array type is, in most contexts, implicitly converted to a pointer to the array's first element. This does not create a pointer object, it merely generates a pointer value; no pointer object is allocated unless you actually create one.

Read section 6 of the comp.lang.c FAQ -- and avoid any tutorial that falsely claims that "arrays are really pointers".

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

In you sample. array is a memory address, and the various indexes of the array represent offsets from that address. (e.g. array[0] = the value at address of array, with no offset)

cmsjr
  • 56,771
  • 11
  • 70
  • 62
0

Well, array[10] is exactly the same as *(array + 10). If you use that formula, you get the value. In C, char array[20] is an array of actual valuas, rather than pointers. char *array[20] would be an array of 20 char pointers.

Guntram
  • 414
  • 2
  • 5
  • 18