Home > other >  Assign an array of strings that is returned by a function to a new array in C
Assign an array of strings that is returned by a function to a new array in C

Time:11-06

I am making a qna game that will take 5 random questions from a pool of 10 and present them to the user.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char* questions(){
    
    static char* er[10] = { //strings in "" are stored in memory, pointer tells the compiler to access the content of said memory
    "2 2", //ans 4
    "4-5", //ans -1
    "10*10", //ans 100
    "17*3", //ans 51
    "9/3", //ans 3
    "45 24 35-68", //ans 36
    "4-2", //ans 2
    "592-591", //ans 1
    "8 3", //ans 11
    "9*9" //answer 81
};
    return *er;
}

int main() 
{
    int i;
    char *erts;
    erts = questions();
    for(i = 0; i<10; i  ){
        printf("%s", erts[i]);
    }
    
    return 0;
}

The program compiles without any errors or warnings, but the array isn't being passed to erts. When I run it, it doesn't print anything, instead it takes a couple seconds before exiting with a value of 3221225477. I want to pass the array from questions() to erts in main but I'm not sure how.

CodePudding user response:

If er is an array of ten char * (pointer-to-char), then *er is a single char *. This is why there are no warnings - returning a char * from a function declared as char *questions(); is perfectly valid as far as the type system is concerned.

As is assigning that pointer value to char *erts. The problem of course is that this is a single pointer-to-char, and thus erts[i] is a single char.

The loop attempts to print individual characters from this string. You should receive a warning here, with a decent compiler, as the printf specifier for a single character is %c, not %s. This invokes Undefined Behaviour.

You want to return the correct type and address for the entire array. The array will decay to a pointer to its first element when returned from a function, so the type becomes char **.

Change the appropriate lines to:

  • char **questions(void),
  • return er;, and
  • char **erts = questions();
#include <stdio.h>

#define QUESTION_SIZE 10

char **questions(void)
{
    static char *er[QUESTION_SIZE] = {
        "2 2",
        "4-5",
        "10*10",
        "17*3",
        "9/3",
        "45 24 35-68",
        "4-2",
        "592-591",
        "8 3",
        "9*9"
    };

    return er;
}

int main(void)
{
    char **erts = questions();

    for (size_t i = 0; i < QUESTION_SIZE; i  )
        puts(erts[i]);
}
  • Related