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.

5
  • "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. Commented Jan 28, 2018 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 Commented Jan 28, 2018 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. Commented Jan 28, 2018 at 17:30
  • If you assume that A is always the same as A+0 no I assumed A as &A[0] Commented Jan 28, 2018 at 17:32
  • &A[0] is the same as A+0 in this case. Commented Jan 28, 2018 at 17:35

3 Answers 3

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.
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.