S
Sona
I understand the problem I'm having but am not sure how to fix it. My
code passes two char* to a function which reads in some strings from a
file and copies the contents into the two char*s. Now when my function
returns, the values stored in the char* are some garbage values (perhaps
because I didn't allocate any memory for them).. but even if I allocate
memory in the function, on the return of this function I see garbage..
here is my code:
typedef char* string;
int main(int argc, char* argv[]) {
parseFile(argv[1], strX, strY);
...
... // values of strX and strY are garbage
}
void parseFile(const string file, string source, string target){
//read in some stuff from the file
source = malloc((size_t)sizeof(char)*X_SIZE_READ_FROM_FILE);
target = malloc((size_t)sizeof(char)*Y_SIZE_READ_FROM_FILE);
strcpy(source, STRING_READ_FROM_FILE);
strcpy(target, STRING_READ_FROM_FILE);
// print source and target... they're correct
}
can someone tell me what to do?
Thanks
Sona
code passes two char* to a function which reads in some strings from a
file and copies the contents into the two char*s. Now when my function
returns, the values stored in the char* are some garbage values (perhaps
because I didn't allocate any memory for them).. but even if I allocate
memory in the function, on the return of this function I see garbage..
here is my code:
typedef char* string;
int main(int argc, char* argv[]) {
parseFile(argv[1], strX, strY);
...
... // values of strX and strY are garbage
}
void parseFile(const string file, string source, string target){
//read in some stuff from the file
source = malloc((size_t)sizeof(char)*X_SIZE_READ_FROM_FILE);
target = malloc((size_t)sizeof(char)*Y_SIZE_READ_FROM_FILE);
strcpy(source, STRING_READ_FROM_FILE);
strcpy(target, STRING_READ_FROM_FILE);
// print source and target... they're correct
}
can someone tell me what to do?
Thanks
Sona