gpt4 book ai didi

C 中指针与整数的比较

转载 作者:行者123 更新时间:2023-11-30 21:46:25 24 4
gpt4 key购买 nike

我试图在链表中搜索星号,但是每次我尝试使用 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);
}

最佳答案

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

/*
*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 != '*')

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

其他内容只是结构的基础知识以及如何访问其成员。

关于C 中指针与整数的比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10367061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com