I have the following code:
void dereference_pointer(int* pointer){
pointer = (int[]){1, 2, 3};
printf("%d | %d | %d\n", pointer[0], pointer[1], pointer[2]);
// This prints "1 | 2 | 3", like I wanted
}
int main(void) {
int *pointer;
pointer = malloc(sizeof(int)*3);
dereference_pointer(pointer);
printf("%d | %d | %d\n", pointer[0], pointer[1], pointer[2]);
// This prints "0 | 0 | 0", essentially undoing what happened inside the function
return 0;
}
I'm not entirely sure pointer = (int[]){1, 2, 3}; is the correct way of pointing a pointer to a new array, but that's the only way I could do it that wouldn't give me a type error.
[1, 2, 3]outside the function