Reputation: 35
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.
Upvotes: 1
Views: 1639
Reputation: 284
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.
Upvotes: 1