1

There is an array A, defined as int A[10], and there is another pointer containing the base address, defined as int *ptr = A.

I recently saw this post How to find the 'sizeof' (a pointer pointing to an array)?, and I can't figure out what the difference between A and ptr is. Aren't both of them just possessing the base address of the array?

We can use both A[i] and ptr[i] interchangeably now, for some integer i less than 10.

So why is there a difference in result obtained on using sizeof()?

Community
  • 1
  • 1
Dhruv Mullick
  • 551
  • 9
  • 25
  • 5
    The difference is one is an array and the other is a pointer. That's why there is a difference in `sizeof`. They are different types. – juanchopanza Feb 01 '15 at 18:24
  • 1
    So if you have `int B[20];` and do `ptr = B;` somewhere in the program after your `int *ptr=A;` you expect the compiler to "remember" that `ptr` now points to `B` and tell you it's `20 * sizeof(int)`? What if you do `ptr = &A[5];`? What is `sizeof(ptr)` now? What if you do `ptr = malloc(50000);`? or `ptr = (int *)0x124418;` [the address of a hardware register]? – Mats Petersson Feb 01 '15 at 18:28
  • 2
    I'm not so sure this is really a duplicate. The C++ array FAQ is of course authoritative, but I wrote a main part of it, so my opinion that it's not a duplicate should have some weight. The C question is about a different language, and yes there are very relevant differences. Voting to reopen. – Cheers and hth. - Alf Feb 01 '15 at 18:34
  • The point is that an array will *decay* to a pointer to its first element in some situations, e.g. when it is passed to a function. – Dabbler Feb 01 '15 at 18:37
  • @Dabbler When passed to a function *that expects a pointer*. – juanchopanza Feb 01 '15 at 18:57

2 Answers2

5

The array is an array. An array is a contiguous sequence of items in memory. For an array of n items the size reported by sizeof is n times the size of each item.

The pointer is a pointer. A pointer value is (in practice) the memory address of something. The size of a pointer is essentially the size of a memory address.

In some context an expression that refers to an array, decays to a pointer to the first item of the array, and that, combined with support for e.g. indexing notation used with pointers, can make pointers seem similar to arrays. The decays does not happen when you e.g. pass an array by reference, or where you use it as argument to sizeof, but it does happen when you e.g. add an integer to an array, like "Hello"+2. That doesn't make sense for an array as such, so the array expression decays (to a pointer type that can serve as argument to the built-in +).

On top of that, in some contexts an array type is adjusted to pointer type. For example, a function with signature void foo(int a[42]); is adjusted to void foo(int* a);. This means the function can be called with any pointer to int, regardless of whether it points to an element in an array. Array decay means you can call the function passing it the name of an array, but the type of a in the function is int*.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

Pointer ptr is pointing to the location in memory of an A array. Array A contains all elements

That is why array takes more memory then pointer, pointer only contains adress of array A

Think of it like shortcut ptr on desktop and real location of file A, using both you can run program

wedo
  • 129
  • 1
  • 11