Reputation: 4163
I am passing a C Macro to a function which receives it as char *
. Without any reason the last character from macro gets truncated. I doubt some memory leak, but could not find where.
#define FROM "/some/local/path/from/"
#define TO "/some/local/path/to/"
....
char file[_D_NAME_MAX + 1] = {'\0'};
....
funMove(file, FROM, TO);
....
....
int funMove(char *file, char *from, char *to) {
//here the to value is one character less (/some/local/path/to) slash got truncated
}
Upvotes: 0
Views: 255
Reputation: 4163
Apologies!! Actually, nothing was wrong with the code and the macro didn't get truncated but got overridden. There was another macro with the same name and content except the slash. So, the macro was got replaced instead of the intended one.
#define TO "/some/local/path/to" //header file 1
#define TO "/some/local/path/to/" //header file 2
I just had the header file 2 in mind and misunderstood that the macro got truncated. Actually, the macro from header file 1 was used instead of file 2 which was the intended one.
Thanks for all your answers and support.
Upvotes: 0
Reputation: 882028
There's nothing wrong with the code you've shown us since the following works fine:
#include <stdio.h>
#include <string.h>
#define _D_NAME_MAX 50
#define FROM "/some/local/path/from/"
#define TO "/some/local/path/to/"
char file[_D_NAME_MAX + 1] = {'\0'};
int funMove(char *file, char *from, char *to) {
printf ("[%s] [%s] [%s]\n", file, from, to);
return 0;
}
int main (void) {
strcpy (file, "fspec");
int x = funMove(file, FROM, TO);
printf ("[%d]\n", x);
return 0;
}
It outputs:
[fspec] [/some/local/path/from/] [/some/local/path/to/]
[0]
as expected, so there must be a problem elsewhere if you're seeing to
truncated.
Upvotes: 4