The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

C arrays vs pointers

After all these years I thought I understood pointers. Sniff.
Kartik Agaram
Tuesday, September 12, 2006
The name of the array is a pointer to the first element -
this doesn't mean that arrays and pointers are the same thing.
Martin Send private email
Tuesday, September 12, 2006
This was and probably is still one of the most common assumptions that a char* and a char[] are the same.  Even the K&R book states they are, until your turn the page over and continue the paragraph.  In that they are only the same when seen from inside the body of a function.  Outside where they are declared, they are definately different beasts.
Gareth Lewis
Tuesday, September 12, 2006
Array is just a label, which addresses the memory location.
Pointer is a storage, where the address for memory location stored. However, when writing code, names of a pointer and array can be used in same fashion to access memory.

int a [4];
int* p = a + 2; // array name used in pointer math


class A;

void foo (A& rA)

A* pA = new A;
foo (pA [0]); // pointer used as an array
asmguru62 Send private email
Tuesday, September 12, 2006
An array is basically a const pointer.  You can't change the address.  So you can't use an array ref on the LHS of assignment.  See
Honu Send private email
Tuesday, September 12, 2006
an array isnt exactly a const pointer, though it can be used like one. as pointed out above:

array_name ---> [array data]
pointer_name ---> [address] ---> [array data]

though i think that passing an array to a function converts it to a pointer?
Tuesday, September 12, 2006
There's a comment in that thread that explains it clearly - and better than I probably would have managed.

Arrays have special magic done by the compiler, basically to avoid allocating RAM for an unnecessary (and implicit) pointer. It's not surprising that this can get confusing if you don't pay careful attention to the difference between "char *array;" (explicit pointer) and "char array[10];" (implicit pointer). But there's still a pointer, which is why passing arrays to functions tends to look a lot like passing pointers around.

Tuesday, September 12, 2006
A C array, such as foo bar[32]; is of type foo[32], not type *foo.  C arrays *decay* to a pointer as neccessary.

At that point, sizeof() no-longer returns the size of the array, but the the size of pointer, because it no-longer has type foo[32], but type *foo.
Arafangion Send private email
Tuesday, September 12, 2006
The name of the array is not a const pointer. It is CAST to a pointer when it is used like a pointer.
Meghraj Reddy
Tuesday, September 12, 2006

foo a[n];

has space for n foos.

foo *p;

does not (at least not yet). Makes a difference when you:

*a = foo1;
*p = foo2;
Robert Send private email
Wednesday, September 20, 2006
Recently I was coding a program that loads a couple of bitmap images and draws them to the screen, and by accident something really weird happened.

I wanted to store the filenames of the bitmaps my program would be loading into an array. When I declared the array the first time, I knew it needed to be static, but I forgot to add in the actual type of the array.
  static image[4]; 
  image[1] = "spaceship.bmp";
  image[2] = "starsclose.bmp";
  image[3] = "starsmid.bmp";
  image[4] = "starsfar.bmp";

This code worked without a problem. Later though I re-worked the array and made it global. When I did this I removed the static identifier and what a surprise I got when I tried to compile.

Here is the really weird part though.

  char image[4];

This did not work!
(I understand now, only one string fits into an array of char array[n])
Though I am not sure why, this code works (with warnings).

  int image[4];

Now, I am not an expert on C by a long shot, but this is awfully confusing.

What is the compiler doing? Does it make each string in the array into a numerical value? ("spaceship.bmp" would become 39487473 or something). I sure don't know. The code works. Can someone explain to me why int image[4] would store strings properly, I don't have a clue.
Thursday, October 05, 2006

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz