Error : format'%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=]

后端 未结 3 1108
半阙折子戏
半阙折子戏 2021-01-24 10:17

I am currently trying to do my own shell, and it has to be polyglot. So I tryed to implement a function that reads the lines in a .txt file.

#include 

        
3条回答
  •  无人共我
    2021-01-24 10:58

    The contents of your text file have nothing to do with the warning, which is generated by the compiler before your program ever runs. It is complaining about this statement:

    printf("%s\n%s\n", aMsg[HI], aMsg[QUIT]);
    

    Global variable aMsg is an array of char, so aMsg[HI] designates a single char. In this context its value is promoted to int before being passed to printf(). The %s field descriptor expects an argument of type char *, however, and GCC is smart enough to recognize that what you are passing is incompatible.

    Perhaps you had in mind

    printf("%s\n%s\n", &aMsg[HI], &aMsg[QUIT]);
    

    or the even the equivalent

    printf("%s\n%s\n", aMsg + HI, aMsg + QUIT);
    

    but though those are valid, I suspect they won't produce the result you actually want. In particular, given the input data you specified and the rest of your program, I would expect the output to be

    HQ
    Q
    

    If you wanted to read in and echo back the whole contents of the input file then you need an altogether different approach to both reading in and writing out the data.

提交回复
热议问题