c - 结构中的语义错误

标签 c string pointers if-statement structure

该代码的目的是存储图书卡信息并使用作者的姓氏搜索书籍。当我使用作者姓名搜索时,它显示“未找到该作者姓氏的书籍”。下面是我的代码。我在这里做错了什么?

//Program to store Library card Information and search for record using last name
#include<stdio.h>

void search(struct lib_card *name)

struct author
{
    char fname[20];
    char lname[20];
};

struct pub_date
{
    int day;
    int month;
    int year;
};

struct lib_card //Structure declaration for storing Library card information
{
    char title[80];
    struct author name;
    struct pub_date date;
};

void main()
{
    struct lib_card card[2];
    int l; //l is a variable assigned for loop
    for (l = 0; l < 2; l++)
    {
        printf("********Enter Library Card Information********\n");
        printf("Card # %d\n", l + 1);
        printf("Enter Book Title:             ");
        scanf("\n%[^\n]s", card[l].title);

        printf("Enter Author's First name:  ");
        scanf("\n%s", card[l].name.fname);

        printf("Enter Author's Last name:   ");
        scanf("\n%[^\n]s", card[l].name.lname);

        printf("\nEnter Published date:\n");
        printf("\t\tdate(dd):");
        scanf("\n%d", &card[l].date.day);

        printf("\t\tMonth(mm):");
        scanf("\n%d", &card[l].date.month);

        printf("\t\t\Year(yy):");
        scanf("\n%d", &card[l].date.year);
    }

    search(card); //Structure's address is passed to search function
}

void search(struct lib_card *name)
{
    char llastname[20];
    int l;

    printf("\nEnter Author's Last name to search for books:");
    scanf("\n%[^\n]s", llastname); //Get author's last name to search the books

    for (l = 0; l < 2; l++)
    {
        printf("%s\n", (name + l)->name.lname);
        if ((name + l)->name.lname == llastname)
        {
            printf("%s\n", (name + l)->title);
        }
        else if (l == 1)
        {
            printf("\nNo books by this Author's Last name found");
        }
    }
}

最佳答案

这不是比较字符串的正确方法:

if ((name + l)->name.lname == llastname)

使用strcmp相反

关于c - 结构中的语义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31971787/

相关文章:

c - C 中带有 %g 的字符串格式

python - Linux epoll 系统调用,等待数据可用

swift - 无法将类型 'Never' 的返回表达式转换为返回类型 'String'

c# - 使用基本字符串操作的字符串模式匹配

string - Golang 中的 string 和 .String() 问题

c - 如何按值传递 char ** 指针?

c++ - 通过来自另一个类的指针访问时包含 "random"值的类的数组属性

c++ - 如何理解独立 C 或 C++ 实现中的原子?

c - C 编程中的 fread 函数

c++ - 为什么 C++ 中仅有时使用 & 运算符来分配指针?