Can anyone please explain?
char *s = get_string("s: ");
if (!s)
{
return 1;
}
// allocate memory for another string
char *t = malloc((strlen(s) +1) * sizeof(char));
if (!t)
{
return 1;
}
if I understand correctly.
After an input from a user a memory has been allocated for "*s".
Then we declare a new variable called "t" that's going to store the address of a character.
char *t = malloc((strlen(s) +1) * sizeof(char));
"Take length of s. Add 1 byte for \0. Call it t and allocate a separate memory for this."
But why do we need sizeof(char) here? Didn't we already know the length of this string form *s?