this is my code:
void init_array(int** array) {
*array = (int*) malloc(3 * sizeof(int));
/* ???? **(array+2) = 666; */
return;
}
void init(int* array, int* length) {
*length = 3;
*(array+0) = 0;
*(array+1) = 1;
*(array+2) = 2;
return;
}
int main(void) {
/* Variables */
int array_length;
int* array;
/* Initialize */
init_array(&array);
init(array, &array_length);
free(array);
return 0;
}
My question is: How can I initialize values of the array in a function init_array().
I have attempted things such as:
**(array+2) = 666;*(*(array+2)) = 666;*array[2] = 666;**array[2] = 666;
When I used pencil and paper I came to result that **(array+2) should work but it gives me a segmentation fault.
I would appreciate your answer because I am confused how pointers in C actually work.
(*array)[2] = 666;and compile with all warnings on.newoperator or what ever to declare an object and then create the values in the specified address.