简体   繁体   English

传递给C函数时char数组的值丢失

[英]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. 我试图将char []传递给函数,但是每次尝试传递char []都会失去它的价值。 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. 行通过测试,我可以访问该字符串,但是当传递到rd_field_length时,它将丢失其值。

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. 如果您有32位整数和64位指针,则精度损失很可能导致指针为空或无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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