Answers to this question describe how to allocate memory and return it to the calling function. An example was given:
void someFunction (int **data) {
*data = malloc (sizeof (int));
}
void useData (int *data) {
printf ("%p", data);
}
int main () {
int *data = NULL;
someFunction (&data);
useData (data);
return 0;
}
I would also like to assign values before returning to the calling function. However, when I try (for example):
void someFunction (int **data) {
*data = malloc (2 * sizeof (int));
*data[0] = 1;
*data[1] = 1;
}
void useData (int *data) {
printf ("%p", data);
}
int main () {
int *data = NULL;
someFunction (&data);
useData (data);
return 0;
}
I receive a segmentation fault. Any help is appreciated.