简体   繁体   中英

Losing value of char array when passing to C function

I'm trying to pass a char[] to a function however every time that I try to it loses it's value. I can pass it to other functions, but when I pass it to one specific one it just loses the string stored in the array. code follows:

int test(char one[], int len) {
    printf("The string: %s, The size: %d", one, len);
    int to = rd_field_length(one, 0, len);
    return to;
}
int main(){
    rd_open("thefile");
    char line[200] = "";
    double d;
    int * err;
    int c, length = 0;
    if(fp != NULL) {
            while((c = rd_getchar()) != EOF && c != '\n'){
                     line[length] = c;
                     length++;
            }
    }
    int to = test(line, length);
}
int rd_field_length(char buf[], int cur, int end ){
    printf("BUF: %s", buf);
    return 0;
}

Line gets passed to test and I can access the string, however when passing to rd_field_length it loses it's value.

As you don't have a prototype in scope and the function is called before it is defined, variables will be passed as integers. If you have 32 bit integers and 64 bit pointers, the loss of precision will likely result in a null or invalid pointer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM