-
pass by refrence help.
The function takes a string pointer and returns by refrence a string . I can print the string just fine in the function inself but when i try to print it from the main function it prints some junk. Can any body tell me why?? Pleaseeeeeeeeeeeeeeeeeeeeeeeeeeeeee
Code:
int validateInptFile(char *fPtr, char **seqNo){
char fileName[18], sNo[10];
strncpy(sNo, &fileName[9], 4);
sNo[4] = '\0';
*seqNo = &sNo[0];
printf("seqNo > %s\n", *seqNo);
//output "0001"
}
//function call from main
validateInptFile(newAddFile, &curSeqNo));
printf("MAIN > %s\n", curSeqNo);
//outputs some junk
-
1) References don't exist in C. You are using a pointer to get to the data
2) You are trying to use a pointer to a local nonstatic variable that is destroyed after the function end
-
Simpler is:
Code:
int validateInptFile(char *fPtr, char *seqNo){
char fileName[18], sNo[10];
strncpy(sNo, &fileName[9], 4);
sNo[4] = '\0';
strcpy(seqNo, sNo);
printf("seqNo > %s\n", seqNo);
//output "0001"
}
Or even simpler:
Code:
int validateInptFile(char *fPtr, char *seqNo){
char fileName[18];
strncpy(seqNo, &fileName[9], 4);
seqNo[4] = '\0';
printf("seqNo > %s\n", seqNo);
//output "0001"
}
And then the call would be:
Code:
validateInptFile(newAddFile, curSeqNo));