如何从链表中获取字符串的第一个字符?

时间:2016-04-23 19:04:09

标签: c struct linked-list nodes

我有这行代码:

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++;
            }
        }       
    }

...

1 个答案:

答案 0 :(得分:0)

我假设您希望搜索查询中的起始空格作为外卡执行。您的strcmp需要稍加更新一下:将C替换为if (strcmp(n->data.name[i], searchName[i]) == 0)