-3

My program compiles with cc but when I compile it with $ gcc -pedantic -Wall -ansi -O4 I get warnings that I don't understand:

miniShell.c: In function ‘main’:
miniShell.c:144:24: warning: initializer element is not computable at load time [enabled by default]
                 struct command cmd[] = { {printenv}, {sort}, {less} };

What does the above mean? I have my declarations on the top of main for these variables:

        char *printenv[] = { "printenv", 0};
        char *sort[] = { "sort", 0 };
        char *less[] = { "less", 0 };
        struct command cmd[] = { {printenv}, {sort}, {less} };

I have a global variable for command.

struct command
{
    char * const *argv;
};
3
  • @GregHewgill They are declared like this: char *printenv[] = { "printenv", 0}; May 17, 2015 at 3:34
  • 1
    Show the definition of struct command.
    – R Sahu
    May 17, 2015 at 3:35
  • @RSahu struct command cmd[] = { {printenv}, {sort}, {less} }; May 17, 2015 at 3:36

1 Answer 1

2

The value of printenv varies at runtime. Subsequent executions of your program may find that the location of that array changes.

In C89, the elements of an initializer list must be constant compile time expressions. Which that isn't.

In C99 and later, this restriction was relaxed. You can enable this with -std=c99 or -std=c11

6
  • How can I achieve it in C89? May 17, 2015 at 4:02
  • 1
    It's not legal in C89. There are gcc extensions that support it, but -pedantic explicitly turns them off...
    – Bill Lynch
    May 17, 2015 at 4:03
  • Then what are my options if I must compile with -pedantic if I want to rewrite my program to compile with -pedantic ? May 17, 2015 at 4:05
  • Ok. Maybe it will work using cmd[0].argv= printenv; cmd[1].argv= sort; cmd[2].argv= less; May 17, 2015 at 4:20
  • 1
    @Niklasinstockholm why don't you want to use -std=c11 ?
    – M.M
    May 17, 2015 at 5:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.