0

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.

Chinna
  • 3,930
  • 4
  • 25
  • 55

2 Answers2

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