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.

5
  • 1
    Since char* is used for one string, how can char* questions() return an array of them? Commented Nov 5, 2022 at 16:30
  • The questions function should return er as a char **... Commented Nov 5, 2022 at 16:33
  • @Serge Ballesta what difference does that make? Commented Nov 5, 2022 at 16:46
  • It is indeed the same address value, but not the same type... And for the very same reason, erts should also be a char **. Commented Nov 5, 2022 at 16:50
  • When I use 2 pointers instead of one I get warnings for "Initialization from incompatible pointer type" followed by "(near initialization from er[0])" for all items in the array and then a final warning "return from incompatible pointer type" Commented Nov 5, 2022 at 16:51

1 Answer 1

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]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

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
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 is equivalent to printf("%s\n", a_string) - printing a string argument followed by a newline to stdout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.