2

I have a struct that looks like this.

struct puzzle {
  int d[16]; 
}; 

I heard that arrays and pointers are the same in C/C++, so I thought that the struct would store a pointer, and the pointer points to an int array. However, I did simple experiments using a debugger to see how exactly is it stored, and I found out that the array is directly stored in the struct.

  1. Why isn't the array pointer stored in the struct?
  2. Where is the pointer stored at?
legends2k
  • 31,634
  • 25
  • 118
  • 222
user3323258
  • 71
  • 1
  • 2
  • 7
  • 5
    `I heard that arrays and pointers are the same in C/C++`...time for C-FAQ. – Sourav Ghosh Aug 13 '15 at 10:37
  • Also, `struct` behave differently in `C` and `C++`, choose either. – Sourav Ghosh Aug 13 '15 at 10:38
  • When you write int[16] , the compiler knows your struct will always have an array of 16 integers allocated when it's created . So it's cheaper to store the int[16] directly in the object than create a pointer and allocate the array somewhere in the heap. – Saiph Aug 13 '15 at 10:46
  • This is one of the most consistently misunderstood and mis-*taught* features of C and C++. Arrays *are not* pointers, an array object *does not* store a pointer value. Array *expressions* will be converted to pointer expressions under most circumstances, but that's not the same as *being* a pointer. – John Bode Aug 13 '15 at 13:47

5 Answers5

5

I heard that arrays and pointers are the same in C/C++

No! They're very different. An array expression decays into a pointer in many instances, but that's about it. Beyond that arrays and pointers are very different creatures. Understand more about the decaying nature of arrays to avoid confusions like this: What is array decaying?

Why isn't the array pointer stored in the struct? Where is the pointer stored at?

The array is the member of the struct and it's stored as expected. The decayed pointer is obtained implicitly, there's nothing to store here.

struct puzzle s;
int *p = s.d;     /* p is now pointing to s.d[0] */

Here s.d gets implicitly converted to int*. Where this decay happens and where it doesn't depends on the language in question. In C++ there're more instances than in C. This is another reason why not to tag a question both C and C++.

Community
  • 1
  • 1
legends2k
  • 31,634
  • 25
  • 118
  • 222
  • 2
    Nit: "An array decays to a pointer" should be "An array *expression* decays to a pointer". The array object itself never changes type. – John Bode Aug 13 '15 at 13:44
1

I heard that arrays and pointers are the same in C/C++!!!!!

Arrays

An array is a fixed-length collection of objects, which are stored sequentially in memory.

Pointers

A pointer is a value that refers to another object (or function). You might say it contains the object's address.

Arrays decay to pointer (implicit pointer conversion ) when they are passed to functions.

Why isn't the array pointer stored in the struct?

Just because the postman know the address of your house, will you let him stay with you?? Only the members of your family can stay, right? Same in your case, the array is the member of struct.

Nishant
  • 1,635
  • 1
  • 11
  • 24
1

Arrays and pointers are different things in C.

In many cases array variables can be treated as pointers, e.g. when passed as arguments to a function taking a pointer:

void f(int *p);

main() {
    int a[3] = {1, 2, 3};
    f(a);
}

But arrays themselves are not pointers at all, they are continuous pieces of memory allocated somewhere (in your case inside a struct).

szx
  • 6,433
  • 6
  • 46
  • 67
1

Arrays and pointers are different types. An array is a named or unnamed extent of memory allocated for its elements.

A pointer is an object that stores an address.

For example if you execute this statement

std::cout << sizeof( puzzle ) << std::endl;

for a structure declared like this

struct puzzle {
  int d[16]; 
};

then the output will be at least not less than the value 16 * sizeof( int ).

If you will execute the same statement

std::cout << sizeof( puzzle ) << std::endl;

for a dtructure declared like this

struct puzzle {
  int *d; 
};

then the output will be at least equal to the value sizeof( int * ).

Arrays are implicitly converted to pointers to their first elements when they are used in expressions.

For example

int a[16];
int *p = a;

Here in the second declaration array a used as initializer is converted to pointer to its first element.

There is no difference between using an array or a pointer with the subscript operator like

a[i]

or

p[i]

because in the both cases this expression is evaluated like

*( a + i )

or

*( p + i ) 

that is again the array is converted to pointer to its first element and there is used the pointer arithmetic in the expression.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Arrays have sequential access.a[0] location is =100 means ,a[1] would be in 101, a[2] in 102. Pointers are not sequential they are randomly stored based on the address.

RAJKUMAR NAGARETHINAM
  • 1,408
  • 1
  • 15
  • 26