I just wanted to know that, Is there any difference between the definitions int arr;
and int arr[1];
(except the accessing, i know we need to access as arr
and arr[0]
). According to my knowledge both are allocating same size of memory (sizeof(int)
). Is there any other details about these allocations.
Asked
Active
Viewed 189 times
0

Chinna
- 3,930
- 4
- 25
- 55
-
1The type of `arr` is different in each case. – juanchopanza Nov 10 '14 at 06:59
-
1The only thing same about the two is that they need the same amount of memory. Everything else is different. They are different types, they need to be used differently. – R Sahu Nov 10 '14 at 07:02
-
1I suppose that when `arr[1]` is a local variable it will always be allocated on stack, it can't be allocated in a register. I do not see other difference in space allocation at the moment. – Marian Nov 10 '14 at 07:04
-
@Marian Thanks for comment. this is what i'm expecting. – Chinna Nov 10 '14 at 07:07
-
@Marian: An array can be defined with the `register` keyword; you just can't index it (because you can't take its address). – Keith Thompson Nov 10 '14 at 07:11
-
Side note - you could access `arr[0]` as `*arr` too. – ChiefTwoPencils Nov 10 '14 at 10:11
2 Answers
0
int arr;
Here arr
is a variable of type int.sizeof(arr) will return size of the integer.
int arr[1];
Here arr
is a array of type int which can hold elements of type int. In this case it can hold only one integer element.sizeof(arr[0]) will return size of first element of the array which is an integer.
For both the local variable and array the memory is allocated on stack.

Gopi
- 19,784
- 4
- 24
- 36
0
i think int arr[1]
allocate more memory than int arr
the fact that int arr[1]
create a pointer on the first element. So you have the arr
pointer, that is a registred value of &arr[0]
and the arr[0] value.
Also you can access arr[0] directly by *arr

crashxxl
- 682
- 4
- 18