0

I have this string malloced one byte:

char * word = malloc(sizeof(char));

I have another string source.content. The first few characters of this string needs to be copied to word and I use a loop to do the work:

for (int i = 0; isalnum(source.content[0]); ++i)
{
    word[i] = source.content[0];
    source.content += 1;
}

After the loop, I terminate word:

word[strlen(token.value)-1] = '\0';

My question is, why does the program above work when I copied more than 1 character to word even though it's only malloced for 1 character? When I realloaced an additional byte every loop, it does the same thing just slower.

SomBoii
  • 77
  • 1
  • 8

0 Answers0