I do not understand why arr is not pointing to the array I have made inside the function test:
void test(int *arr)
{
int tmp[] = {2,4};
arr = tmp;
printf("%d %d\n",arr[0],arr[1]);
}
int main()
{
int *arr;
test(arr);
printf("%d %d\n",arr[0],arr[1]);
return 0;
}
When I run this program I got:
2 4
17744 786764
arr is pointer so why is not the values updated?
UPDATE: I see now that I had misunderstand a lot about pointers and variable local to a function. Have updated my small dumb script to something that works:
#include <stdio.h>
#include <stdlib.h>
void test(int *arr){
arr[0] = 123;
arr[1] = 321;
printf("%d %d\n",arr[0],arr[1]);
}
int main()
{
int arr[2];
test(arr);
printf("%d %d\n",arr[0],arr[1]);
return 0;
}
testfunction...arrinmainget updated?