1

i have often heard that array and pointers can be used interchangeable in some situation but the two does not mean the same thing so what are the circumstances in which we can use array as a pointer and vice versa.

OldSchool
  • 2,123
  • 4
  • 23
  • 45

3 Answers3

3

Arrays and pointers are never the same thing.

However, under certain circumstances, an array name in your code will "decay" to a pointer to the first element. That means you lose information about the size of the array since a pointer doesn't know how many elements it points to (technically, it only points at one though you can advance through a contiguous array if you can tell where the end is, such as with a length or sentinel value).

Situations in which arrays do not behave like pointers are (for example):

  • when you do a sizeof: for the array, it's the size of the entire array, for a decayed pointer, it's the size of the pointer.
  • when you want to move through an array: with a real array, you must use indexing while you can simply increment the pointer.

Consider the following code:

#include <stdio.h>

void fn (int arr[]) {
    printf ("sz = %d\n", sizeof(arr));
    printf ("#4 = %d\n", arr[4]);
    arr = arr + 1;
    printf ("#4 = %d\n", arr[4]);
}

int main (void) {
    int x[] = {1,2,3,4,5,6,7,8,9};
    printf ("sz = %d\n", sizeof(x));
    printf ("#4 = %d\n", x[4]);
    //x = x + 1; // Cannot do this
    printf ("#4 = %d\n", x[4]);
    puts("=====");
    fn(x);
    return 0;
}

which outputs:

sz = 36
#4 = 5
#4 = 5
=====
sz = 4
#4 = 5
#4 = 6

You can see from that the sizeof is different and you can actually move the pointer whereas the array name is at a fixed location (you'll get an error if you uncomment the line that tries to increment it).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

The name of an array behaves pretty much like a pointer to the first element. That is it's value, although it has other attributes that are different.

This is most obvious when calling a function. If you have:

float sum_floats(const float *x, size_t num_values);

you can call it with:

float three[] = { 1.f, 2.f, 3.f };
const float sum = sum_floats(three, sizeof three / sizeof *three);

Note how three in the function call "decays" into &three[0], i.e. a pointer to the first element in the array. Note also how sizeof three still works, since three really is an array.

Inside the function, the array has decayed into const float *, the type of the function's argument, and you can no longer use sizeof to get the size of the caller's array (since the function has no idea that the caller used an array).

unwind
  • 391,730
  • 64
  • 469
  • 606
0

Typically an array is a container for a number of elements of the same type, while a pointer is the memory address for a memory location that contains a specific value.

When you declare an array like this:

int arr[] = {1, 2, 3, 4, 5};
printf("%d", *arr); /* will print 1 */
printf("%d", arr[0]); /* will print 1 as well */
/*or*/
int arr[5]; 

you are allocating memory for 5 integers. Take care that the array name by itself acts as a pointer to the first element in the array.

You can achieve the same thing using pointers:

int* arr = new int[5];
Rami
  • 7,162
  • 1
  • 22
  • 19