Hello,
I have a function that I would like to copy the contents off the pointer array into the output pointer array (and change the contents). However, it works, but I get a stack dump when I try and display the contents in the main function.
Many thanks for any suggestions,
Code:
void display_names(char **names_to_display, char **output);
int main(void)
{
char *names[] = {"Luke", "John", "Peter", 0};
char **my_names = names;
char *new_output[] = {0};
size_t i = 0;
while(*my_names)
{
printf("Name: %s\n", *my_names++);
}
my_names = names; /* Reset */
display_names(my_names, new_output);
// Stack dump here.
while(*new_output[i])
{
printf("Full names: %s\n", *new_output[i]);
i++;
}
getchar();
return 0;
}
void display_names(char **names_to_display, char **output)
{
while(*names_to_display)
{
*output = malloc(strlen("FullName: ") + strlen(*names_to_display) + 1); // Allocate memory
// Copy new output
sprintf(*output, "FullName: %s", *names_to_display++);
printf("display_names(): Name: %s\n", *output++);
}
}