0

I have learned that we can use the address of elements of an array at index i as &A[i] or simply as A+i and to get the value A[i] or *(A+i) where A is an array and i denotes the Index. Let's say int A[]={1,2,3};

When I use sizeof(*A) I get value 4, now when I use sizeof(A) I must get the size of address value of the first element why I get the size of the whole array as 12. I am a beginner and confused, please guide.

anime
  • 118
  • 10
  • "when I use sizeof(A) I must get the size of address value" no, you should get the size of A. Since A is an array, you are getting the size of the array. – n. m. could be an AI Jan 28 '18 at 17:14
  • Since &A[i] is same as (A+i) , so if I do sizeof(A) , I must get the capacity of address pointing to array's first element , hope you understand – anime Jan 28 '18 at 17:16
  • The rules of the language are such that if you say `sizeof(A)`, you get the size of `A`. Not the size of `(A+0)` and not the size of `&*A`. If you assume that A is always the same as A+0 etc. then you are mistaken. – n. m. could be an AI Jan 28 '18 at 17:30
  • `If you assume that A is always the same as A+0` no I assumed `A` as `&A[0]` – anime Jan 28 '18 at 17:32
  • `&A[0]` *is* the same as `A+0` in this case. – n. m. could be an AI Jan 28 '18 at 17:35

3 Answers3

2
int A[]={1,2,3};

A is an array and array is not a pointer. when you are printing sizeof(*A) it prints 4 because *A means first element of array which size is 4.

sizeof(A) will results not result in 4 because A is not a pointer. A is an array and array means collection of elements and each elements needs 4 bytes.

A[i] == *(A+i)
  • sizeof(A[i]) => 4 because A[0] is an integer
  • sizeof(*A) => 4 because *A means value at starting address and that needs 4 bytes to store
  • sizeof(A) => 12 bytes, not 4 because A is not a pointer.
  • sizeof(&A[0]) => 4 bytes because &A[0] yields in address and size of any address will be 4 bytes.
Achal
  • 11,821
  • 2
  • 15
  • 37
0

When you call sizeof(A) you get the size of the array as n.m said. But If you initialize your array as this:

    int *A = new int[3];
    A[0] = 1;
    A[1] = 2;
    A[2] = 3;
    cout << sizeof(A) << endl;

you will get 4.

wdc
  • 2,623
  • 1
  • 28
  • 41
  • Since `A` is equal to `&A[0]` for an array, so if I do `printf("%d",A)` I will get the address of first value in my case, so why does `sizeof(A)` don't function in same manner and tells me the sizeof address value pointing to first element of A. – anime Jan 28 '18 at 17:19
0

When I use sizeof(*A) I get value 4

This is nothing but size of first element in the array ie sizeof(*A) is the same as sizeof(*(&A[0])).


now when I use sizeof(A) I must get the size of address value of the first element why I get the size of the whole array as 12

You have declared A as

int A[]={1,2,3};

Now, ask yourself a simple question - What is A ? Never settle with anything other than Array for an answer. In short you get the size of entire array in bytes. Here you have 3 elements which takes 4 bytes each.

Added

I must get the size of address value

That should be sizeof(&A) - which is the size of the address of A - which is usually 8 bytes in many systems.

sjsam
  • 21,411
  • 5
  • 55
  • 102