【问题标题】:How do I get the first character of a string from a linked list? [closed]如何从链表中获取字符串的第一个字符? [关闭]
【发布时间】:2016-04-23 19:04:09
【问题描述】:

我有这行代码:

if (strcmp(n->data.name[i], searchName[i]) == 0)

我确定它不正确,我如何检查存储在链表中的名称的第一个字符?(错误是“n->data.name[i]” 谢谢

这是剩下的代码:

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

typedef struct record
{   
    char name[20];
    char surname[20];
    char telephone[20];
}Record;

typedef struct node
{
    Record data;
    struct node *next;
}Node;

Node *head = NULL;


void search() {
    Node *n = head;
    Node *next = n;
    int valid = 0;
    int length2;
    int valid2 = 0;
    int valid3 = 0;
    int count = 0;
    char searchName[20];

    printf("  Enter name, and/or surname, or tel no.   : ");
    gets();
    gets(searchName);

    length2 = strlen(searchName);
    for (int i = 0; i < length2; i++)
    {
        valid2 = 0;
        while (isspace(searchName[i]));
        {
            if (strcmp(n->data.name[i], searchName[i]) == 0)
            {
                valid2 = 1; 
                count++;
            }
        }       
    }

...

【问题讨论】:

  • 试试n-&gt;data.name[i]
  • 取决于您对链表的实现。显示结构声明。
  • 如果n-&gt;data.name是一个字符串,那么n-&gt;data.name[i]是一个单一的字符,你不要用string比较那些功能。
  • n-&gt;data.name)[i] - 这绝对是不是完整的错误消息,应该总是报告完全和完全在您的配置中显示。除了此处提供的单行代码外,我们对您的代码一无所知。我们只看到您向我们展示的内容;我们不介意读者。
  • 我不会称之为停止

标签: c struct linked-list nodes


【解决方案1】:

我假设您希望搜索查询中的起始空格用作通配符。然后你的 strcmp 需要一个小更新:用 if (strcmp(n-&gt;data.name + i, searchName +i) == 0) 替换 if (strcmp(n-&gt;data.name[i], searchName[i]) == 0)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 2015-05-21
    • 2011-10-29
    • 2021-04-15
    相关资源
    最近更新 更多