I have an array that I created with malloc.
int *array = (int *) malloc(10 * sizeof(int));
I want to use this array in function.And I want to affect values of the array in main function. Like a pointer
void func(int **array);
How can I do this correctly?
First thing that comes to mind(working):
void func(int **arr){
arr[0][0] = 100;
arr[0][1] = 200;
}
int main(){
int *arr = (int*) malloc(10 * sizeof(int));
func(&arr);
printf("arr[0] = %d\narr[1] = %d\n", arr[0], arr[1]);
return 0;
}
Second(Not work):
typedef int* intarr;
void func(intarr *arr){
*arr[0] = 100;
*arr[1] = 200;
}
int main(){
intarr arr = (int*) malloc(10 * sizeof(int));
func(&arr);
printf("arr[0] = %d\narr[1] = %d\n", arr[0], arr[1]);
return 0;
}
My first question is why am I getting a segmentation error in the second method?
My second question is how accurate is the first method?
My third question is what are you using?