Using Double Pointers after memory allocated within function

前端 未结 3 1870
自闭症患者
自闭症患者 2020-11-28 16:41

I was playing with double pointers in C and was wondering if I create a function that initializes the table, it crashes on going back to main when I try to make use of the m

3条回答
  •  猫巷女王i
    2020-11-28 17:21

    You have a pointer issue.

    It's like if you say:

    void inc(int a){
        a++;
    }
    
    int main(){
        int a = 0;
        inc(a);
        printf ("%d\n", a); // will display 0, not 1
    }
    

    does not work.

    You must pass &strTable instead of strTable as InitStringTable argument, and change other things in InitStringTable consequently ..
    Or just do strTable = InitStringTable(); , and return a char** from InitStringTable.

提交回复
热议问题