user981234
user981234

Reputation:

Array syntax and pointers in C

Is my understanding of arrays in C correct? Arrays are nothing more than a syntactic convenience such that, for instance, when you declare in your C code an array:

type my_array[x];

the compiler sees it as something equivalent to:

type *my_array = malloc(sizeof(*my_array) * x);

with a free system call that releases my_array once we leave the scope of my_array.

Once my_array is declared

my_array[y];

is nothing more but:

*(my_array + y)

Transposing this to character strings; I was also wondering what was happening behind the curtain with

char *my_string = "Hello"

and

my_string = "Hello"

Upvotes: 0

Views: 164

Answers (3)

phuclv
phuclv

Reputation: 41764

No!

type array[n] is a variable stored on stack

type *array is a pointer variable stored on the stack too. But after array = malloc(sizeof(*array) * n); it'll point to some data on the heap

Upvotes: 1

Keith Thompson
Keith Thompson

Reputation: 263227

No, an array object is an array object. C has some odd rules that make it appear that arrays and pointers are the same thing, or at least very similar, but they very definitely are not.

This declaration:

int my_array[100];

creates an array object; the object's size is 100 * sizeof (int). It does not create a pointer object.

There is no malloc(), even implicitly. Storage for my_array is allocated the same way as storage for any object declared in the same scope.

What may be confusing you is that, in most but not all contexts, an expression of array type is implicitly converted to a pointer to the array's first element. (This gives you a pointer value; there's still no pointer object.) This conversion doesn't happen if the array expression is the operand of a unary & or sizeof. &my_array gives you the address of the array, not of some nonexistent pointer obejct. sizeof my_array is the size of the entire array (100 * sizeof (int)`), not the size of a pointer.

Also, if you define a function parameter with an array type:

void func(int param[]) { ... }

it's adjusted at compile time to a pointer:

void func(int *param) { ... }

This isn't a conversion; in that context (and only in that context), int param[] really means int *param.

Also, array indexing:

my_array[3] = 42;

is defined in terms of pointer arithmetic -- which means that the prefix my_array has to be converted to a pointer before you can index into it.

The most important thing to remember is this: Arrays are not pointer. Pointers are not arrays.

Section 6 of the comp.lang.c FAQ explains all this very well.

Once my_array is declared

my_array[y];

is nothing more but :

*(my_array + y)

Yes, because my_array is converted to a pointer, and the [] operator is defined so that x[y] means *(x+y).

Transposing this to character strings; i was also wondering what was happening behind the curtain with

char *my_string = "Hello"

and

my_string = "Hello"

"Hello" is a string literal. It's an expression of type char[6], referring to an anonymous statically allocated array object. If it appears on the RHS of an assignment or initializer, it's converted, like any array expression, to a pointer. The first line initializes my_string so it points to the first character of "Hello". The second is a pointer assignment that does the same thing.

So what about this?

char str[] = "Hello";

This is the third context in which array-to-pointer conversion doesn't happen. str takes its size from the size of the string literal, and the array is copied to str. It's the same as:

char str[] = { 'H', 'e', 'l', 'l', 'o', '\0' };

Upvotes: 2

Lajos Arpad
Lajos Arpad

Reputation: 76434

If it walks like a duck, swims like a duck and flies like a duck, then it is a duck.

So, let's see. Arrays and pointers have some common attributes as you correctly described, however, you can see there are some differences. Read more here.

Upvotes: 0

Related Questions