Quick question on malloc and strings
I'm trying to get the hang of using pointers for strings instead of arrays. I'm still finding it a little confusing. Just wondering if I could get some feedback - does this look OK? It works so far, but ....
Code:
int length;
char *word;
void addWord(const char *newWord){
length = strlen(newWord) +1;
word = NULL;
word = (char *) malloc(length);
if (word != NULL)
{
strlcpy(word, newWord, length); //strlcpy adds '\0'
}
else
{
printf("\nCannot find space for word");
exit(0);
}
}