Char * (pointer) function

后端 未结 4 706
悲&欢浪女
悲&欢浪女 2020-12-10 10:04

I need to pass in a char * in a function and have it set to a cstring value. I can properly set it as a string in the function, but it doesn\'t seem to print out correctly i

4条回答
  •  时光取名叫无心
    2020-12-10 10:14

    You have to pass a pointer to a pointer.

    int l2_read(char **chunk,int length)
    {
        *chunk = malloc( sizeof(char) * length);
    
         int i;
         for(i = 0; i < length; i++)
         {
             char c;
             if (read(&c) < 0) return (-1);
             (*chunk)[i] = c;
         }
    
         printf("%s",*chunk);
         return 1;
    
    }
    
        char *string;
        int value = l2_read(&string,16);
        printf("%s",string);
    

提交回复
热议问题