I encountered erratic behavior when initializing array in function.
I have an uninitialized pointer below:
int *arr;
And I am passing it to a function for initialization.
init_arr(&arr);
void init_arr(int **arr)
{
*arr = (int *) calloc(10, sizeof **arr);
}
The pointer is initilized, and when I try get items *arr[2] and larger I get the error:
Cannot access memory to address 0x0
The source code was compiled using gcc version 4.8.4
(*arr)[2]perhaps?(*arr)[2]is equivalent to justarr[2], as the compiler treats a pointer as an array.calloc()why? because 1) the returned value is a defined asvoid *so can be assigned to any other pointer. 2) the cast just clutters the code and can create a real headache when debugging or maintaining the code.