I'm trying to remove empty spaces from a char with pointer.
char * byte="a b c d"
char * CopiedByte;
for (;*byte;++byte)
{
if(*byte!=' ')
*CopiedByte=*byte;
}
printf("%s",CopiedByte);
But i dont get anything in output
I'm trying to remove empty spaces from a char with pointer.
char * byte="a b c d"
char * CopiedByte;
for (;*byte;++byte)
{
if(*byte!=' ')
*CopiedByte=*byte;
}
printf("%s",CopiedByte);
But i dont get anything in output
You have not allocated memory to CopiedByte
and once you copy the char from byte
to CopiedByte
you need to increment the CopiedByte
to point to next location.
Important note: Once you copy all the data put the NULL
terminator.
Memory allocated using malloc
(dynamically) need to be freed otherwise there will be memory leaks
.
char * byte="a b c d";
char * CopiedByte = malloc(strlen(byte) +1);
int i=0;
for (;*byte;++byte)
{
if(*byte!=' ')
{
CopiedByte[i]=*byte;
i++;
}
}
CopiedByte[i] = '\0';
/* do your work*/
free(CopiedByte);