Char * (pointer) function

2019-01-18 17:43发布

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 in the function that called the char * function in the first place.

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); // this gets a single character
          chunk[i] = c;
    }

    printf("%s",chunk); // this prints fine
    return 1;
}


    // main
    char *string;
    int value = l2_read(string,16);
    printf("%s",chunk); // prints wrong

标签: c char
4条回答
贪生不怕死
2楼-- · 2019-01-18 17:52

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);
查看更多
贼婆χ
3楼-- · 2019-01-18 17:55

In C, everything is passed by value. A general rule to remember is, you can't change the value of a parameter passed to a function. If you want to pass something that needs to change, you need to pass a pointer to it.

So, in your function, you want to change chunk. chunk is char *. To be able to change the value of the char *, you need to pass a pointer to that, i.e., char **.

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

and then in main():

 char *string;
 int value = l2_read(&string, 16);
 if (value == 1) {
     printf("%s", string); /* corrected typo */
     free(string); /* caller has to call free() */
 } else if (value == -2) {
    /* malloc failed, handle error */
 } else {
    /* read failed */
    free(string);
 }

Pass-by-value in C is the reason why strtol(), strtod(), etc., need char **endptr parameter instead of char *endptr—they want to be able to set the char * value to the address of the first invalid char, and the only way they can affect a char * in the caller is to receive a pointer to it, i.e., receive a char *. Similarly, in your function, you want to be able to change a char * value, which means you need a pointer to a char *.

Hope that helps.

查看更多
家丑人穷心不美
4楼-- · 2019-01-18 18:05

I just re-read your question.

You seem to have been hit by the pass by value, even if it is a pointer, problem. Also, is chunk null terminated?

查看更多
混吃等死
5楼-- · 2019-01-18 18:10

I totally agree with the answer posted above. You are essentially modifying the value of pointer so you need to pass the reference of pointer. use char ** instead of char*.

查看更多
登录 后发表回答