Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays with 0 elements

Is an array with 0 elements the same as an unallocated pointer?

Is int arr[0]; the same as int* arr;?

Edit: What if I did something similar to this:

int x[0];
int* const arr = x;

I tried this code and it compiled. To my knowledge, both x and arr should be pointing to the same location in memory. What would be the difference in this case?

like image 663
Dasaru Avatar asked Apr 20 '12 15:04

Dasaru


1 Answers

Not at all.

In the case of arr[0], arr has a well defined address.

In the case of *arr, arr is just uninitialized.

After your EDIT, where you initialize the const arr with an array defined just before : there would just be no difference in the content of the variables, but in the actions you would be allowed to perform on them.

like image 194
Skippy Fastol Avatar answered Oct 27 '22 17:10

Skippy Fastol