警告:当使用gcc-std=c89-g–;编译时,来自不兼容指针类型的赋值;迂腐的文件名.c
我正在使用链表进行队列实现。。需要使用命令gcc-std=c89-g–pedantic filename.c在omega上编译代码而不发出警告。但是,我收到警告:来自行号(174223253)上不兼容指针类型[默认启用]的赋值。此外,我在将记录打印到文件中时也面临问题:警告:当使用gcc-std=c89-g–;编译时,来自不兼容指针类型的赋值;迂腐的文件名.c,c,pointers,c89,C,Pointers,C89,我正在使用链表进行队列实现。。需要使用命令gcc-std=c89-g–pedantic filename.c在omega上编译代码而不发出警告。但是,我收到警告:来自行号(174223253)上不兼容指针类型[默认启用]的赋值。此外,我在将记录打印到文件中时也面临问题: #include <stdio.h> #include <errno.h> #include <stdbool.h> #include <stdlib.h> #include &l
#include <stdio.h>
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
struct student_record
{
int student_id_;
int student_age_;
char first_name_[21];
char last_name_[21];
};
struct student_record_node
{
struct student_record* record_;
struct student_record_node* next_;
}*front,*rear,*temp,*front1;
void enq(struct student_record* sr);
void parseFile(char* filename, struct student_record_node** head);
void printNode(struct student_record_node* node);
struct student_record_node* student_record_allocate();
void student_record_node_deallocate(struct student_record_node* node);
void sortByAge(struct student_record_node** recordsHead);
void sortById(struct student_record_node** front);
void swap(struct student_record_node** node1, struct student_record_node** node2);
int main ( int argc, char *argv[] )
{
char *filename = argv[1];
int i;
/**for(i=0;i<argc;i++)**/
/** printf("%d %s ",i,argv[i]);**/
/**printf( "usage: %s filename", argv[1] );**/
parseFile(filename,&front);
printNode(front);
printf("\n");
printf("Sorting by age \n");
sortByAge(&front);
printNode(front);
printf("Sorting by id \n");
sortById(&front);
printNode(front);
/**student_record_node_deallocate(front);**/
getch();
return 0;
}
void swap(struct student_record_node** node1, struct student_record_node** node2)
{
struct student_record *s1;
struct student_record_node *t1 = *node1,*t2=*node2;
/**s1=(struct node *)malloc(1*sizeof(struct student_record));**/
s1= t1 -> record_;
t1->record_= t2->record_;
t2->record_=s1;
}
void sortByAge(struct student_record_node** front)
{
int swapped, i;
struct student_record_node *ptr1;
struct student_record_node *lptr = NULL;
struct student_record *s1,*s2;
/**Checking for empty list**/
if (ptr1 == NULL)
return;
do
{
swapped = 0;
ptr1 = *front;
while (ptr1->next_ != lptr)
{
s1=ptr1->record_;
s2=ptr1->next_->record_;
if (s1->student_age_ > s2->student_age_)
{
swap(&ptr1, &ptr1->next_);
swapped = 1;
}
ptr1 = ptr1->next_;
}
lptr = ptr1;
}
while (swapped);
}
void sortById(struct student_record_node** front)
{
int swapped, i;
struct student_record_node *ptr1;
struct student_record_node *lptr = NULL;
struct student_record *s1,*s2;
/**Checking for empty list**/
if (ptr1 == NULL)
return;
do
{
swapped = 0;
ptr1 = *front;
while (ptr1->next_ != lptr)
{
s1=ptr1->record_;
s2=ptr1->next_->record_;
if (s1->student_id_ > s2->student_id_)
{
swap(&ptr1, &ptr1->next_);
swapped = 1;
}
ptr1 = ptr1->next_;
}
lptr = ptr1;
}
while (swapped);
}
void student_record_node_deallocate(struct student_record_node* node)
{
front1 = node;
struct student_record_node* temp;
if ((front1 == NULL) && (rear == NULL))
{
printf("Queue is empty");
return;
}
while (front1 != rear)
{
temp =front1;
front1 = front1->next_;
free(temp);
}
if (front1 == rear)
{
free(front1);
}
free(node);
free(temp);
}
struct student_record_node* student_record_allocate()
{
struct student_record_node* sr;
sr = (struct node *)malloc(1*sizeof(struct student_record_node));
return sr;
}
void printNode(struct student_record_node* node)
{
front1 = front;
struct student_record* s;
if ((front1 == NULL) && (rear == NULL))
{
printf("Queue is empty");
return;
}
while (front1 != rear)
{
s=front1->record_;
printf("struct student_record_node\n");
printf("\tstudent firstname %s\n", s->first_name_);
printf("\tstudent lastname %s\n", s->last_name_);
printf("\tstudent id %d\n", s->student_id_);
printf("\tstudent age %d\n\n", s->student_age_);
front1 = front1->next_;
}
if (front1 == rear)
{
s=front1->record_;
printf("struct student_record_node\n");
printf("\tstudent firstname %s\n", s->first_name_);
printf("\tstudent lastname %s\n", s->last_name_);
printf("\tstudent id %d\n", s->student_id_);
printf("\tstudent age %d\n\n", s->student_age_);
}
}
void enq(struct student_record* sr)
{
if (rear == NULL)
{
rear = student_record_allocate();//(struct node *)malloc(1*sizeof(struct student_record_node));
rear->record_ = sr;
rear->next_ = NULL;
front = rear;
}
else
{
temp=(struct node *)malloc(1*sizeof(struct student_record_node));
rear->next_ = temp;
temp->record_ = sr;
temp->next_ = NULL;
rear = temp;
}
}
void parseFile(char* filename, struct student_record_node** head)
{
struct student_record * sr;
char item[21], status[21];
int id,age;
FILE *fp;
if((fp = fopen(filename, "r+")) == NULL) {
printf("No such file\n");
exit(1);
}
while (true) {
int ret = fscanf(fp, "%s %s %d %d", item, status, &id, &age);
if(ret == 4)
{
/**printf("\n%s \t %s %d %d", item, status, id, age);**/
sr = (struct node *)malloc(1*sizeof(struct student_record));
strncpy(sr->first_name_, item, 21);
/**sr->first_name_ = item;**/
strncpy(sr->last_name_,status,21);
/**sr->last_name_ = status;**/
sr->student_id_=id;
sr->student_age_=age;
enq(sr);
}
else if(errno != 0) {
perror("scanf:");
break;
} else if(ret == EOF) {
break;
} else {
printf("No match.\n");
}
}
printf("\n");
/*if(feof(fp)) {
puts("EOF");
}*/
}
你不应该投马洛克的回归 所以,更改第174行的代码
sr = (struct node *)malloc(1*sizeof(struct student_record_node));
到
对所有三行重复此操作。除此之外,排序似乎还可以
要打印到文件,您可以使用函数
fprintf(fp,“…”)
而不是使用printf您不应该强制执行malloc的返回
所以,更改第174行的代码
sr = (struct node *)malloc(1*sizeof(struct student_record_node));
到
对所有三行重复此操作。除此之外,排序似乎还可以
要打印到文件,您可以使用函数fprintf(fp,“…”)
而不是使用printf(mmm…),因为您正在分配不兼容的指针类型
struct student_record_node* sr;
sr = (struct node *)malloc(1*sizeof(struct student_record_node));
这里,您将malloc
的返回值强制转换为“指向结构节点的指针”,然后将其分配给“指向结构学生记录节点的指针”
事实上,演员阵容很糟糕。扔掉它。malloc
的返回值是void*
,它与任何其他指针兼容。此外,还可以使用位置作为sizeof
的参数,这有其优点。1*
只是杂乱无章。因此,您只剩下:
struct student_record_node *sr = malloc(sizeof *sr);
错误就会消失
然后检查返回值是否为空。尽早养成真正程序员的习惯,即使这只是一个玩具
而且学习c89
语法也不是最好的主意,尤其是作为一名新程序员。在非平凡的情况下,它实际上需要糟糕的风格。为什么你认为gcc
需要一个标志来强制它?我知道这可能超出你的控制范围,但我会向老师提出
“面对问题”不是问题。如果您请求特定的帮助,您通常会在这里得到它。这是(mmm…),因为您正在分配不兼容的指针类型
struct student_record_node* sr;
sr = (struct node *)malloc(1*sizeof(struct student_record_node));
这里,您将malloc
的返回值强制转换为“指向结构节点的指针”,然后将其分配给“指向结构学生记录节点的指针”
事实上,演员阵容很糟糕。扔掉它。malloc
的返回值是void*
,它与任何其他指针兼容。此外,还可以使用位置作为sizeof
的参数,这有其优点。1*
只是杂乱无章。因此,您只剩下:
struct student_record_node *sr = malloc(sizeof *sr);
错误就会消失
然后检查返回值是否为空。尽早养成真正程序员的习惯,即使这只是一个玩具
而且学习c89
语法也不是最好的主意,尤其是作为一名新程序员。在非平凡的情况下,它实际上需要糟糕的风格。为什么你认为gcc
需要一个标志来强制它?我知道这可能超出你的控制范围,但我会向老师提出
“面对问题”不是问题。如果你需要特别的帮助,你通常会在这里得到。检查这行
if (ptr1 == NULL)
return;
它应该是“前”而不是“ptr”检查这一行
if (ptr1 == NULL)
return;
它应该是“前端”而不是“ptr”这些行在哪里?如果您可以制作一个小得多的示例来说明问题,那就太好了。此代码在多个位置错误地使用了
strncpy
。您应该避免使用此功能,并且应该在fscanf中对%s
使用长度保护。swap
功能未正确实现,此逻辑会破坏列表。(画一幅图让自己相信这一点)注意,这个问题与这些线在哪里密切相关?如果您可以制作一个小得多的示例来说明问题,那就太好了。此代码在多个位置错误地使用了strncpy
。您应该避免使用此功能,并且应该在fscanf中对%s
使用长度保护。swap
功能未正确实现,此逻辑会破坏列表。(画一张图让自己相信这一点)注意,这个问题与printNode()
函数密切相关,它忽略了它的参数,总是转储整个列表,这不是一个好的设计。应该有一个“打印一个节点”函数和另一个“打印列表中的所有节点”函数,该函数反过来应该重复调用一次打印一个节点的函数。我怀疑还有其他问题-在这个问题的前一个版本中肯定有。这里不能同意:您的建议消除了警告,但代码仍然分配了错误的字节数。它应该是铸造版本,但铸造到正确的类型;或者(我的偏好)sr=malloc(sizeof*sr)
printNode()函数忽略其参数并总是转储整个列表,这不是一个好的设计。应该有一个“打印一个节点”函数和另一个“打印列表中的所有节点”函数,该函数反过来应该重复调用一次打印一个节点的函数。我怀疑还有其他问题-在这个问题的前一个版本中肯定有。这里不能同意:您的建议消除了警告,但代码仍然分配了错误的字节数。它应该是铸造版本,但铸造到正确的类型;或者(我的偏好)sr=malloc(sizeof*sr)代码>注意:这是一个错误,但还有几个错误。注意:这是一个错误,但也有几个错误