C - malloc array in function and then access array from outside

后端 未结 5 1031
孤独总比滥情好
孤独总比滥情好 2021-01-03 11:44

Here is how I malloc an int var and then access this var outside of the function

int f1(int ** b) {
  *b = malloc(sizeof(int)); 
  **b = 5;
}

int main() {
          


        
5条回答
  •  盖世英雄少女心
    2021-01-03 12:16

    Instead of malloc()ing a single int, malloc the array.

    int f1(int * b) {
      b = malloc(ARRAY_LENGTH * sizeof(int)); 
      *b = 5;
      b[1] = 6;
      *(b + 2) = 7;
    }
    

提交回复
热议问题