I'm trying to assign a char value to an char pointer. The problem is
when I comment
Code:
char *a = "Some Birds Can't Fly";
the printed display is however without commenting above, it gives, lot of scrambled characters. Can you check the code and let me know what's happening.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *assign_char(char *str, char ch)
{
size_t len = strlen(str);
char *strRslt = malloc( len + 2 );
strcpy(strRslt, str);
strRslt[len] = ch;
strRslt[len+1] = '\0';
return strRslt;
}
int main(void)
{
char *b;
char *a = "Some Birds Can't Fly";
char ch = 'B';
b = assign_char(b, ch);
printf("%s\n", b);
return 0;
}
compiled with