How can I return an array of strings in an ANSI C program?
For example:
#include<stdio.h>
#define SIZE 10
char ** ReturnStringArray()
{
//How to do this?
}
main()
{
int i=0;
//How to do here???
char str ** = ReturnStringArray();
for(i=0 ; i<SIZE ; i++)
{
printf("%s", str[i]);
}
}
You could do the following. Error checking omitted for brevity
Note that you’d need to correspondingly free the returned memory.
Additionally in your printf call you’ll likely want to include a
\nin your string to ensure the buffer is flushed. Otherwise the strings will get queued and won’t be immediately printed to the console.