Javier
Javier

Reputation: 4623

Are pointers and arrays any different in C?

I'm writing a small C program to do some number crunching, and it needs to pass around arrays between functions. The functions should accept and return pointers, right?

For example, this (I know it may not be the most efficient thing):

int* reverse(int* l, int len) {
    int* reversed = malloc(sizeof(*reversed)*len);
    int i, j;
    for (i = 0, j = len-1; i < len; i++, j--) {
        reversed[j] = l[i];
    }
    return reversed;
}

Am I using pointers right?

Upvotes: 7

Views: 1010

Answers (5)

Aditya Sehgal
Aditya Sehgal

Reputation: 2883

Your code snippet is correct. However, pointers and arrays in C are indeed different. Put simply "the pointer to type T" is not same as "the array of type T".

Please have a look at C Faq discussing Pointers & arrays to get a better understanding of this.

Upvotes: 14

Mark Edgar
Mark Edgar

Reputation: 4797

A link to C-FAQ: Arrays and Pointers has been given already, but there is also a thorough explanation in C for Smarties: Arrays and Pointers. You may want to read the "Analyzing Expressions" page first.

A couple of things about your example which should be corrected:

  1. Using int instead of the correct size_t for storing object sizes.
  2. Failure to check the return value of malloc().

One way to "fix" this second problem is to let the caller decide where the output is stored (perhaps the caller does not want or need a freshly allocated array):

int *reverse(int *out, int *l, size_t len) {
   size_t i, j;
   for (i = 0, j = len - 1; i < len; ++i, --j) {
     out[j] = l[i];
   }
   return out;
 }

Upvotes: 2

Crashworks
Crashworks

Reputation: 41374

In pedantic theory, arrays and pointers are different things, since an array specifies a region of memory and therefore an array's size is part of its type. So one says that an array name decays to a pointer when used in that context. There's a detailed explanation in the C-FAQ.

In practice they're the same since formally a[i] is the same thing as *(a+i), and therefore the compiler back end treats an array name and a pointer in exactly the same way. The only difference worth worrying about is that

void foo()
{
   int a[5]; // allocates five words of space on the stack
   int *b;   // allocates *one* word of space on the stack (to hold the pointer)
}

Your code snippet is fine. Just be careful to free the memory that your function malloc()s in whoever calls it.

Upvotes: 1

Steven A. Lowe
Steven A. Lowe

Reputation: 61223

arrays in C are represented and manipulated by a pointer to the first element, e.g.

int arr[50];
int * ptr1 = arr;
int * ptr2 = &arr[0];

in this example, ptr1 == ptr2 because the address of the first element of the array and the base pointer for the array are the same.

so your basic example is correct, though I suspect that the malloc should probably be:

int* reversed = malloc(sizeof(int)*len);

Upvotes: 0

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421978

From a low level perspective, an array is a continuous chunk of memory that's accessed by the address of the beginning of that chunk and the offset, so from that abstraction level, it is a pointer. However, from C language perspective, an array type is different from a pointer type. Basically an array can be assigned to a pointer value but it's not the same type.

For the purpose of your function, you are doing OK.

Upvotes: 1

Related Questions