The first problem is that xyz
is not being declared as a char *
, only a single char
.
Given the following variable declaration:
char *temp, xyz;
temp
is a pointer to a char
.
xyz
is only a char
.
The *
doesn't apply to both variables. To make both variables pointers to char
, use:
char *temp, *xyz;
Next: you're confusing the usage of sizeof
with strlen
. sizeof
gives you the size occupied by a type or a variable of that type. So if temp
is a char *
, then sizeof(temp)
will be the size of a pointer, not the length of a C string. To get C string lengths - e.g. to get the buffer size that you'll need to copy a given C string - use strlen
.
Finally, unless you've omitted some significant code there's another problem: simply using malloc
doesn't do anything with the newly allocated memory, so after:
temp = (char *)malloc(sizeof(somestring));
temp
points to an uninitialized bit of memory (which is, as mentioned, probably smaller than you think it is due to using sizeof
rather than strlen
). Then you go on to use temp
as if it's a valid C string, with strchr
. This will likely access memory that doesn't belong to you, which invokes undefined behavior.