在C中有strcmp和结构元素的问题(也有带空格的捕获字符串)

问题描述 投票:0回答:1

好吧,我有两个问题,第一个是我不能在函数中使用字符串,如“ One Two”。让我们看看我的代码

case 3:
        goto Cleanup;
        Cleanup: ;
        char nomesq[30];
        printf ("Inserisci il nome della squadra da cercare: ");
        scanf("%[^\n]%*c", nomesq);
        //blablabla other stuff

结果是一个printf,无法插入任何字符,似乎scanf不能正常工作,因为执行过程随程序一起进行并且不会停止(?。?)。我尝试了fgets方法,但这也不起作用。请帮助我,我迷失了方向。


第二个具有相同的功能,但它位于另一个区域。我正在将普通scanf在输入中获取的字符串与结构中的字符串进行比较。这是结构:

typedef struct{     // Tipo giocatore e i suoi dati
        int id;
        char * nome;
        char * cognome;   //<--------- this one (it means surname)
        int eta;
        char * ruolo;
        squadra team;
        char * college;
        int td;
    } giocatore;

这是有效且无效的代码(?。?)。我不知道为什么有时它会起作用,而有时却不起作用,因为这是开关的第二种情况,该开关与第一种情况具有相同的代码,但使用单词“ name”而不是“ surname”。请记住,第一种情况每次都起作用,第二种情况是选择(lol),最后一种情况(3rd)包含我之前解释过的第一个问题。

size_t ricerca(short int tipo, size_t sz, giocatore array[]){
    size_t count = 0;
    char cerca;
    switch(tipo){
    case 1:
        printf ("Inserisci il nome da cercare: ");
        scanf ("%s", &cerca);
        for (size_t i = 0; i < sz; ++i){
            if (strcmp(array[i].nome, &cerca) == 0){
                printf("ID:%d Nome:'%s' Cognome:'%s' Eta:%d Ruolo:'%s' Team:'%s' College:'%s'\n",
                    array[i].id,
                    array[i].nome,
                    array[i].cognome,
                    array[i].eta,
                    array[i].ruolo,
                    array[i].team.nome,
                    array[i].college);
                count++;
                    }
        }
        break;
    case 2:
        printf ("Inserisci il cognome da cercare: ");
        scanf ("%s", &cerca);
        for (size_t i = 0; i < sz; ++i){
            if (strcmp(array[i].cognome, &cerca) == 0){
                printf("ID:%d Nome:'%s' Cognome:'%s' Eta:%d Ruolo:'%s' Team:'%s' College:'%s'\n",
                    array[i].id,
                    array[i].nome,
                    array[i].cognome,
                    array[i].eta,
                    array[i].ruolo,
                    array[i].team.nome,
                    array[i].college);
                count++;
                    }
        }
        break;
    case 3:
        goto Cleanup;
        Cleanup: ;
        char nomesq[30];
        printf ("Inserisci il nome della squadra da cercare: ");
        scanf("%[^\n]%*c", nomesq);
        for (size_t i = 0; i < sz; ++i){
            if (strcmp(array[i].team.nome, nomesq) == 0){
                printf("ID:%d Nome:'%s' Cognome:'%s' Eta:%d Ruolo:'%s' Team:'%s' College:'%s'\n",
                    array[i].id,
                    array[i].nome,
                    array[i].cognome,
                    array[i].eta,
                    array[i].ruolo,
                    array[i].team.nome,
                    array[i].college);
                count += 1;
                    }
        }
        break;
    default:
        printf("Inserisci un valore valido\n;");
        break;
    }
    return count;
}

预先感谢您的耐心和帮助,对不起我的英语:)

c struct strcmp
1个回答
0
投票

“有时有效,有时却无效”。我没有测试,但这意味着undefined behaviour。一个明显的未定义行为是这样的:

char cerca;

然后,此:

scanf ("%s", &cerca);

正在扫描cerca 作为字符上的指针。但是没有足够的空间容纳1个字符(加上nul终止符)。这是一个恶意的错误,因为它甚至不会触发警告(&cerca的类型为char *可以)。

Quickfix:

char cerca[100];

然后

scanf("%99s",cerca);

并在其余代码中将&cerca更改为cercastrcmp部分也应确保启用警告)。这是安全的,如果string太大,则会截断。没有溢出,没有不确定的行为。

© www.soinside.com 2019 - 2024. All rights reserved.