Segmantation fault when trying to access array of character pointers

后端 未结 2 1617
不知归路
不知归路 2021-01-24 10:15

so i\'ve created a function that deals with a array of char pointers by using the [] operators. the function

int fetchargs(char **argv){
argv[0][0] = \'A\';              


        
相关标签:
2条回答
  • 2021-01-24 10:34

    char ** argv is a pointer to a pointer of character(s) or a double pointer
    Where as argv[ARG_NUM][MAX_LINE] is essentially a 2D array of characters

    Bottom line here is to Honor the Data Types

    char argv[ARG_NUM][MAX_LINE];
    .........
    .........
    int fetchargs(char argv[][MAX_LINE]){
      argv[0][0] = 'A'; 
      return 0;  
    };
    
    0 讨论(0)
  • 2021-01-24 10:38

    That's how you should pass it to the function because it isn't a char **. You have to delete the ; after the curly bracket.

    /*Dim is your ARG_NUM and dim2 MAX_LINE*/
    int fetchargs(int dim1,int dim2,char pass[][MAX_LINE]){
        /*Some stuff*/
        return 1;
    }/*You've a semicolon here*/
    
    0 讨论(0)
提交回复
热议问题