views:

104

answers:

3

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]);
    }
}
+2  A: 

You could do the following. Error checking omitted for brevity

char** ReturnStringArray() {
  char** pArray = (char**)malloc(sizeof(char*)*SIZE);
  int i = 0;
  for ( i = 0; i < SIZE; i++ ) {
    pArray[i] = strdup("a string");
  }
  return pArray;
}

Note that you'd need to correspondingly free the returned memory.

Additionally in your printf call you'll likely want to include a \n in your string to ensure the buffer is flushed. Otherwise the strings will get queued and won't be immediately printed to the console.

char** str = ReturnStringArray();
for(i=0 ; i<SIZE ; i++)
{
    printf("%s\n", str[i]);
}
JaredPar
@JMSA added some printing code.
JaredPar
@jaredPar: You failed to fix up the code error hence my -1.... look at the line char str ** = ..... :P
tommieb75
@tommieb75 fixed
JaredPar
@jaredPar: reinstated +1 ;) good man! :)
tommieb75
please don't cast the return of `malloc` in C, otherwise you wouldn't capture the lack of inclusion of the prototype
Jens Gustedt
+2  A: 

Do it this way

#include<stdio.h>

#define SIZE 10

char ** ReturnStringArray()
{
    //How to do this?
    char **strList = (char **)malloc(sizeof(char*) * SIZE);
    int i = 0;
    if (strList != NULL){
         for (i = 0; i < SIZE; i++){
             strList[i] = (char *)malloc(SIZE * sizeof(char) + 1);
             if (strList[i] != NULL){
                sprintf(strList[i], "Foo%d", i);
             }
         }
    }
    return strList;
}

main()
{
    int i=0;

    //How to do here???

    char **str = ReturnStringArray();

    for(i=0 ; i<SIZE ; i++)
    {
        printf("%s", str[i]);
    }
}
  • Problem 1: Your double pointer declaration was incorrect
  • Problem 2: You need to know the size of the string for each pointer in the double-pointer..
  • Problem 3: The onus is placed on you to free the memory when done with it..

The code sample above assumes that the maximum size of the string will not exceed the value of SIZE, i.e. 10 bytes in length...

Do not go beyond the boundary of the double pointer as it will crash

tommieb75
I have done the same thing. But in main() I am writing:- for(i=0 ; i<SIZE ; i++) { printf("%s",ReturnStringArray()[i]); }. This is because, writing char **str = ReturnStringArray();for(i=0 ; i<SIZE ; i++) { printf("%s", str[i]); }is not working. Can you say why? I am using VC++ 2008.
JMSA
@JMSA: `for(i=0 ; i<SIZE ; i++) { printf("%s",ReturnStringArray()[i]); }` - that code is illegal, and does not work...that is, a function with array subscripts which is not the right way :)
tommieb75
A: 

pls dont typecast the return of malloc, you have not included <stdlib.h> and as someone pointed out above lack of prototype will result in int being casted to char **. Accidently your program may or may not work at all.

cyber_raj
But the question doesn't even use `malloc` ? This doesn't seem to answer the question at all.
Charles Bailey
my bad, my reference was to tommieb post.
cyber_raj