0

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.

1 Answers1

1

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]);
}
Oka
  • 23,367
  • 6
  • 42
  • 53
  • Could you explain what puts() and size_t do? I substituted size_t with int and puts() with the equivalent printf() and it works just fine – ArceusLegend50 Nov 05 '22 at 18:11
  • [`size_t`](https://en.cppreference.com/w/c/types/size_t) is the generally appropriate type for indexing arrays - it is essentially an *unsigned* type that is guaranteed to be able to hold the maximum possible value of an array size, and thus any index. `int` is limited by `INT_MAX` which may be smaller than this theoretical, maximal value. It makes no difference in a program this small, but it is best practices. [`puts`](https://en.cppreference.com/w/c/io/puts) is equivalent to `printf("%s\n", a_string)` - printing a string argument followed by a newline to `stdout`. – Oka Nov 05 '22 at 18:50