繁体   English   中英

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

[英]Losing value of char array when passing to C function

我试图将char []传递给函数,但是每次尝试传递char []都会失去它的价值。 我可以将其传递给其他函数,但是当我将其传递给一个特定的函数时,它只会丢失存储在数组中的字符串。 代码如下:

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;
}

行通过测试,我可以访问该字符串,但是当传递到rd_field_length时,它将丢失其值。

由于您在范围内没有原型,并且在定义函数之前就已调用该函数,因此变量将作为整数传递。 如果您有32位整数和64位指针,则精度损失很可能导致指针为空或无效。

暂无
暂无

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

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