-8

我正在尝试通过链表搜索星号,但是每次我尝试使用 while 循环将头指针与星号进行比较时,程序将无法编译,说它无法将指针与整数进行比较

它在打印列表功能中: while( pt != '*')

/*
 *Description: Construction of a social network
 */

#include < stdio.h>
#include < strings.h>
#include < stdlib.h>
#define SIZE 30
#define PEOPLE_SIZE 20
#define PRINT_NETWORK 1
struct people   
{
        char name[SIZE];
        int age;
        char gender[SIZE];
        int idnumber;
        struct friendlist *friends;
};

typedef struct friendlist
{
        int friendsname[PEOPLE_SIZE];
        struct friendlist *next;
        struct people *person;
}node_t;

void scan_friends(FILE *input2, node_t *pt)
{
        char *friend_name;
        fscanf(input2,"%s", &pt->friendsname);
}

void  print_friends(node_t pt)
{
       printf("%s ", pt.friendsname);
}

void print_list(node_t *pt)
{
        int friendsname[PEOPLE_SIZE];
        struct friendlist *next;
        struct people *person;

}node_t;

void  print_friends(node_t pt) 
{
        printf("%s ", pt.friendsname);
}

void print_list (node_t *pt)
{ 
        if (pt==NULL)
                printf("The list is empty\n");
        else
        {                 // traversing the list
                while (pt!=NULL)
                {
                        while (pt != '*')
                       {
                                    print_friends(*pt);
                                    pt=pt->next;
                       }
                }
        }
}

int main(void)
{
    int choice=0;
    FILE *input; //pointer to people.dat

    FILE *input_friends;
    int i=0;
    struct people people[SIZE];
    struct friendlist friendlist[PEOPLE_SIZE];
    node_t *headp, *temp, *current=NULL;
    char user_name[SIZE];
    int user;

            input_friends=fopen("friends.dat", "r"); //opens friends file

            while(!feof(input_friends))
            {
                    // create a new list element
                    temp = (node_t *)malloc(sizeof(node_t));  // memory
                    scan_friends(input_friends, temp);    // initialization of element 
                    temp->next=NULL;     // setting pointer to null.

                    if (current==NULL)
                    {
                            headp=temp;  // setting the head of the list
                    }
                    else
                    {
                                current->next=temp;   // else connecting to previous
                        }
                        current=temp;   // updating the current element

                        i++;   // count number of elements added
                }
                fclose(input_friends);
                print_list(headp);                                    
        }

        printf("\n");
        return(0);
}
4

1 回答 1

2

这是我认为您要发布的内容,但它仍然无法编译,因为您无法将指针与 char 进行比较

/*
 *Description: Construction of a social network
 */

#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#define SIZE 30
#define PEOPLE_SIZE 20
#define PRINT_NETWORK 1

struct people   
{
    char name[SIZE];
    int age;
    char gender[SIZE];
    int idnumber;
    struct friendlist *friends;
};

typedef struct friendlist
{
    int friendsname[PEOPLE_SIZE];
    struct friendlist *next;
    struct people *person;
} node_t;

void scan_friends(FILE *input2, node_t *pt)
{
    char *friend_name;
    fscanf(input2,"%s", &pt->friendsname);
}

void  print_friends(node_t pt)
{
    printf("%s ", pt.friendsname);
}

void print_list (node_t *pt)
{ 
    if (pt==NULL)
        printf("The list is empty\n");
    else
    {                 // traversing the list
        while (pt!=NULL)
        {
            while (pt != '*')
            {
                print_friends(*pt);
                pt=pt->next;
            }
        }
    }
}

int main(void)
{
    int choice=0;
    FILE *input; //pointer to people.dat

    FILE *input_friends;
    int i=0;
    struct people people[SIZE];
    struct friendlist friendlist[PEOPLE_SIZE];
    node_t *headp, *temp, *current=NULL;
    char user_name[SIZE];
    int user;

    input_friends=fopen("friends.dat", "r"); //opens friends file

    while(!feof(input_friends))
    {
        // create a new list element
        temp = (node_t *)malloc(sizeof(node_t));  // memory
        scan_friends(input_friends, temp);    // initialization of element 
        temp->next=NULL;     // setting pointer to null.

        if (current==NULL)
        {
            headp=temp;  // setting the head of the list
        }
        else
        {
            current->next=temp;   // else connecting to previous
        }
        current=temp;   // updating the current element

        i++;   // count number of elements added
    }
    fclose(input_friends);
    print_list(headp);                                    

    printf("\n");
    return(0);
}

我的编辑将其归结为只有一个您需要自己解决的编译错误。它在第 47 行:

while (pt != '*')

这当然不是你想要做的。

您想将什么与星号进行比较?名字?

考虑类似的东西

while (pt->person->name != '*')

请注意,这仍然不起作用,因为您将 char 数组与单个 char 进行比较。但这应该会让你朝着你想去的方向前进,这取决于你想将星号与什么进行比较。请注意,这是您访问具有指针的结构成员的方式。

其他任何东西都只是结构的基础知识以及如何访问它们的成员。

于 2012-04-28T20:40:57.323 回答