"format specifies type" error, C language

Asked

Viewed 70 times

1

I have the following code in C:

struct crPlayList{char music[120], singer[120];float mustime;struct crPlayList *next;};

struct crPlayList *start, *end, *aux;

I have a music search function:

void seacrPlayList(){
aux = start;
// restante do código

Situation line:

scanf("%s", aux);

Returning me the following message:

Warning: format specifies type 'char *' but the argument has type 'struct crPlayList *' [-Wformat] scanf("%s", aux);

And when I search for a song I entered through the responsible function, nothing is found. Someone can help me?

  • But the idea of that scanf was reading the music data ? If yes which ones (since there are several)? But fundamentally the problem is that can not read one struct crPlayList with scanf("%s").

  • 1- The idea was to do a name search and then list the correspondents. 2 - How can I read then? Or there is no way to read from the position itself?

  • scanf read from the console, soon you are introducing a new song I suppose. In this scanf which has Warning you wanted to read the 3 fields of the structure ? to create a new music ? Or that scanf is to read a song name to do the search ? Upon what you are trying to do so the solution differs.

  • The scanf is to read what was typed, and on top of that, search in the field music. I have a while loop, which when it finds correspondents, prints on the screen.

  • while (aux != NULL){
 if (music == aux->music){
 printf("\nMusica  %s encontrada!\n\n", aux->music);
 printf("musica: %s", aux->music);
 printf("cantor: %s\n\n", aux->singer);
 return;
 }else{
 aux = aux->next;
 }
 }

  • So if that scanf is to read the name of a song is just change the type of the aux for a char aux[120] for example.

  • I changed my code. I realized that I had created a variable to store, ams was not using: char busc[120];
 printf("\nDigite o nome da música:");
 scanf("%s", busc);
 
 while (aux != NULL){
 if (busc == aux->music){
 printf("\nMúsica  %s encontrada:\n\n", aux->music);
 printf("Música: %s", aux->music);
 printf("Cantor: %s\n\n", aux->singer);
 printf("Duração: %f\n\n", aux->mustime);
 return;
 }else{
 aux = aux->next;
 }
 }. It does not return me the message, but it rotates and does not find record by name. Remembering, there is a aux = start; before

  • This will certainly be another problem than what you have in the question, and it will only be possible to answer seeing a lot more code of the program. I advise to open another question regarding this new problem. I answer this here to close it and have a solution posted here

  • Ok. I will open a new question, and comment the link of the same here

  • https://answall.com/questions/381252/while-não-encontra-correspondecia-linguagem-c

  • @Itwasn'tme Yes it’s true, I’ve taken the tag cmd

  • Ball show!

Show 7 more comments

1 answer

2


The warning tells you that the read formatter is char* which come from the %s, but the place where you’re holding the value is a struct crPlayList *, soon it will not work as expected as they are completely different types and will result in undefined behavior.

struct crPlayList *aux;
scanf("%s", aux);
//     |      |-----------> tem struct crPlayList *
//     |---> espera char*

You have two solutions, but they depend a little on what you want to do.

If you are reading only a text, such as the name of the song, to search in the list then you must transform the reading variable into an array of char (renamed to nomeProcura for clarity):

char nomeProcura[120];
scanf("%s", nomeProcura);

If you are trying to read a song’s data to insert into the list, you need to read each field individually and save to the correct location:

struct crPlayList *novaMusica = malloc(sizeof(struct crPlayList));
printf("Insira o nome da nova musica\n");
scanf("%s", novaMusica->music);
printf("Insira o cantor da nova musica\n");
scanf("%s", novaMusica->singer);
printf("Insira a duração da nova musica\n");
scanf("%s", &(novaMusica->mustime));
novaMusica->next = NULL;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.