I have a function that I'm using to build a string array. Inside the function i can print the array no worries however the program returns from the called function, the array (arr) remains NULL. How do I get the data out of the array ?
Thanks.
main(){
char **arr = NULL;
funct(arr);
printf("%s\n", arr[2];
}
funct(char **arr){
arr = malloc(10*sizeof(char *));
arr[1...n] = calloc(SIZE, sizeof(char));
// Add data to array
}
char*[]in the function, including both the pointer array and its content, you're missing a level of indirection on your input parameter, though honestly I don't see why you don't just return achar**and call it good.pand you're not dereferencing it at-assignment, likep = <<something>>you're changing nothing on the caller side. In your case,arr = malloc(...allocates memory assigning the result to simplyarr, and promptly leaks it on return.