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()?

5
  • 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. Commented Feb 1, 2015 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]? Commented Feb 1, 2015 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. Commented Feb 1, 2015 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. Commented Feb 1, 2015 at 18:37
  • @Dabbler When passed to a function that expects a pointer. Commented Feb 1, 2015 at 18:57

2 Answers 2

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*.

Sign up to request clarification or add additional context in comments.

4 Comments

There's also extra confusion due to function parameter array types being adjusted to pointers.
@juanchopanza: yes, thanks. please feel free to edit the answer.
Can you explain the decaying, a bit further?
@Cheersandhth.-Alf I gave it a shot. I hope it makes some sense.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.