Skip to content

Conversation

@NieuweNils
Copy link

Hi David!
I am working through your (so far great) book on C (Head First C), and spotted a peculiarity in the code:
in section 2.5, under "It's time for a code review", the code states:

int main()
{
char search_for[80];
printf("Search for: ");
scanf("%79s", search_for);
search_for[strlen(search_for)-1] = '\0';
find_track(search_for);
return 0;
}

where
search_for[strlen(search_for)-1] = '\0'
replaces the last inputted character.
should it be
search_for[strlen(search_for)] = '\0';
instead?

Hi David! 
I am working through your (so far great) book on C (Head First C), and spotted a peculiarity in the code:
in section 2.5, under "It's time for a code review", the code states:

int main()
{
    char search_for[80];
    printf("Search for: ");
    scanf("%79s", search_for);
    search_for[strlen(search_for)-1] = '\0';
    find_track(search_for);
    return 0;
}

where  
    search_for[strlen(search_for)-1] = '\0'
replaces the last inputted character.
should it be 
    search_for[strlen(search_for)] = '\0';
instead?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant