Software and Applications (non-game)
C Programming
C++ Programming
The Difference Between

Difference between pointers and arrays in C?

Answer

Wiki User
10/01/2008

They're related but not totally comparable.

A pointer is an address in memory where a variable is located.

An array is a conceptual data representation consisting of a list of more than one item of a particular scalar type (int, float, char, structure, etc.) where each element is accessed by its index. The index can be thought of as a counter or enumerator telling you how many elements you have to skip over to get to the one you're interested in. Here's where addresses come in ... the location of a particular element in memory is offset from the so-called base address (i.e. the address of the starting element) by the value

(sizeof(one element) * index #)

The C compiler is smart enough to take the sizeof() into account when you do pointer arithmetic, so you can find the address of the i-th element as (address of base) + i, rather than having to do the multiplication explicitly.

The idea of element address as offset also accounts for C's use of zero-relative array definitions. Since the first element is offset by 0 positions from the first element its index in C is 0, not 1. The second (ordinal) element is offset from the first by 1 position so its index is 1, and so on.