dhanraj_kmr
Advanced Member level 4
- Joined
- Sep 23, 2008
- Messages
- 117
- Helped
- 3
- Reputation
- 6
- Reaction score
- 3
- Trophy points
- 1,298
- Location
- Chennai, INDIA
- Activity points
- 2,174
Dear All,
I am trying to concat two strings and store it in third pointer. but After i assign 50 bytes memory to str1, i am not able to write any value to that location.
Can any one explain why my method is wrong?
I am trying to concat two strings and store it in third pointer. but After i assign 50 bytes memory to str1, i am not able to write any value to that location.
Code:
int main(void) {
char *str1;
char *str2 = "Tom";
char *str3,i = 0;
str1 = malloc(50 * sizeof(char *));
if (str1 != NULL){
*str1 = "My name is"; //This line is not effective. why?
str3 = str1;
while(*str1++ != '\0');
-- str1;
while(*str2 != '\0')
*str1++ = *str2++;
*str1 = '\0';
printf("%s\n",str3);
}
return 0;
}
Can any one explain why my method is wrong?