我正在尝试搜索星号的链表,但每次我尝试使用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);
}
答案 0 :(得分: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进行比较。但这应该会让你朝着你想去的方向前进,这取决于你想要比较星号的方向。请注意,这是您访问具有指针的结构的成员的方式。
其他任何事情只是结构的基础知识以及如何访问其成员。