Pointer losing its value + execv compilation warning

前端 未结 1 2028
时光说笑
时光说笑 2020-12-04 04:25

I hope I haven\'t missed a similar question.

I\'m trying to code a mini-shell of my own, using primitive C functions.

I got something that should work, but I

相关标签:
1条回答
  • 2020-12-04 04:29

    C is pass by value. So when doing this

     int searchCmd(char * cmd, char * adrCmd){
    

    adrCmd is a copy of what had been passed in. Overwriting the copy won't change what it had been copied from in the caller.

    To fix this pass down the address of adrCmd:

     int searchCmd(char * cmd, char ** padrCmd){
    

    and use it like this:

        *padrCmd = adr;
    

    Call searchCmd() like this:

      if(!searchCmd(splited[0], &adrCmd)){
    

    and define and initialise adrCmd like this;

      char * adrCmd = NULL;
    
    0 讨论(0)
提交回复
热议问题