How to return a pointer as a function parameter

后端 未结 2 1862
忘了有多久
忘了有多久 2021-02-19 04:59

I\'m trying to return the data pointer from the function parameter:

bool dosomething(char *data){
    int datasize = 100;
    data = (char *)malloc(datasize);
           


        
2条回答
  •  感情败类
    2021-02-19 05:21

    You're passing by value. dosomething modifies its local copy of data - the caller will never see that.

    Use this:

    bool dosomething(char **data){
        int datasize = 100;
        *data = (char *)malloc(datasize);
        return 1;
    }
    
    char *data = NULL;
    if(dosomething(&data)){
    }
    

提交回复
热议问题