0

As the title speaks for itself, I have a hard time to understand the concept of similarity between a pointer and an array.

I understand, that they work with the same logic and that the array variable is a pointer to the address of the first element, but is there any difference between them?

  • 1
    Related: [C: differences between char pointer and array](https://stackoverflow.com/questions/1335786/c-differences-between-char-pointer-and-array). From the [C language standard](http://port70.net/~nsz/c/c11/n1570.html#6.3.2.1p3): "*except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ''array of type'' is converted to an expression with type ''pointer to type'' that points to the initial element of the array object*". – dxiv Mar 14 '21 at 22:43
  • One has storage for data, the other does not:(( – Martin James Mar 15 '21 at 05:37

2 Answers2

2

Arrays and pointers are very different things:

Think of an array as a block of houses in a city and a pointer as a piece of paper with the GPS coordinates of the first house written on it.

The block cannot move, nor can you change its size. Houses can be built on the block (value assignment), which does not change the coordinates on the pointer.

The name of the street (array name) can be used to refer to a given house, but the coordinates are just as efficient, especially for someone who does not have a map (the array name is out of scope).

You can make the pointer point to another house by erasing the coordinates and writing different ones. Writing nothing is similar to making the pointer a null pointer. If the house was destroyed or the street has restricted access, going there with the GPS coordinates may get you in trouble (segmentation fault).

Just looking at the pointer, you cannot tell how many houses are in the block. Getting there and going from house to house may feel safe, but you cannot know where the street ends and another one starts or you may cross the border into enemy territory (out of bounds access, undefined behavior).

The coordinates may be those of any house, not necessarily the first one of the street. Changing them geometrically may get you to the next house if they have a known size (pointer increment).

You can put some pieces of paper in your pockets (CPU registers) or you can stick them on a wall (store them in memory)... At which point they have GPS coordinates themselves which you can write on a piece of paper (pointer to a pointer).

In C, when you pass an array as an argument, the function only gets its address as a pointer, just like the Uber driver gets GPS coordinates.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

You're right an array variable points to its 1st element, but the way it is different than a pointer is that it can't be reassigned to some other address, which also means you can't perform any arithmetic over it to change the address it points to. For ex say int a[] is an array and int* ptr=a is a pointer to this array of int. Now ptr and a both points to the same address now. Lets say there's another array int b[] Now,

ptr=b;  // valid
a=b;    //invalid
ptr++;  //valid
a++;    //invalid