The code performs a fu-yu search for a song . There is an array in the array and the input is what you need to look for, if the input is done through fgets () the program does not work if everything works through scanf () . The question is, why does this happen with fgets () and scanf () and why do we need an asterisk in the global variable char * tracks []?

Here is the whole code

#include <stdio.h> #include <string.h> 

Why is a star here, or rather a pointer, because this is a global variable

  char *tracks[] = { "Я оставил свое сердце в Гарвардской медицинской школе", "Ньюарк, Ньюарк - город, полный чудес town", "Танец с мужланом", "Отсюда и до роддома", "Девченка с острова Иводзима", }; void find_track(char search_for[]) { int i; for (i = 0; i < 5; i++) { if(strstr(tracks[i], search_for)) printf("Песня номер %i: %s\n", i, tracks[i]); } } int main() { char search_for[80];//фу-ия в стеке и яв-ся printf("Искать: "); 

here the essence of the question begins and all the code is in principle correct

  fgets(search_for, 80, stdin); 

the line below counts the number of entered characters takes 1 and it turns out that it adds to the last address in the array \ 0

  search_for[strlen(search_for)-1] = "\0"; 

* fgets for some reason does not work and adds a new character in the code with scanf there is no such problem *

  printf("fgets() приняла: %s;\nДлинна строки: %lu;", search_for, strlen(search_for)); 

The code with scanf () works, but the search in this case will be done only one word at a time.

  scanf("%79s", search_for); find_track(search_for); return 0; } 
  • one
    The compiler pointed out the error here: search_for[strlen(search_for)-1] = "\0"; Did you just ignore his diagnosis? - AnT

2 answers 2

Why is a star here, or rather a pointer, because this is a global variable

  char *tracks[] = { 

Whether a global variable or not changes nothing: this is the definition of an array of pointers to char (characters). In C, in most cases, a pointer to char interpreted as a null-terminated string. Without an asterisk, it would just be an array of characters (or a string).

the line below counts the number of entered characters takes 1 and it turns out that it adds to the last address in the array \ 0

  search_for[strlen(search_for)-1] = "\0"; 

* fgets for some reason does not work and adds a new character in the code with scanf there is no such problem

Basic error: around '\0' should be single quotes that mean a character (character constant). Double quotes mean a string . So in this example, you take the address of a constant string and assign its lower byte to a character, respectively, with a high degree of probability it receives the garbage value. Surely the compiler pointed to this warning, which would be worth listening to. You can also write just 0 (without quotes) - in C, these two constants are absolutely identical.

By the way, a newline character may not be, if the input was completed with EOF or exceeded the buffer length, so among other things, it is worth checking that you are replacing '\ n', and not something else ...

    Well, to the answer @ Fat-Zer I will add that scanf reads one word with %s . If you want to read a string, you need something like

     scanf("%79[^\n]",s);