2015-12-03 62 views
1

我使用链表进行队列实现..需要不使用命令gcc欧米茄警告编译代码在编译时分配从兼容的指针类型 - std = c89 -g -pedantic filename.c。不过,我收到警告:从分配兼容的指针类型[默认启用]就行号(174,223,253)。另外,我面临的问题,在打印记录到文件中:警告:使用gcc -std = C89 -g -pedantic FILENAME.C

#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"); 
    }*/ 
} 

输入文件是:

Joe Smith 00001 24 
Bob Smith 00002 31 
Samantha Smith 00003 30 
Christina Smith 00004 17 
Steven Smith 00005 20 
Jason Smith 00006 3 
Patrick Smith 00007 50 
Alex Smith 00001 29 
+0

那些人在那里线是 “前”?如果你可以制作一个更小的例子来展示这个问题,那就太好了。 –

+0

这段代码在多个地方使用'strncpy'不正确。你应该避免使用这个函数,你应该在fscanf中用'%s'使用长度保护。 –

+0

交换功能没有正确实施,这个逻辑破坏了列表。 (画一张图片来说服你自己) –

回答

0

它(嗯...),因为你要指定一个兼容的指针类型的。

struct student_record_node* sr; 
sr = (struct node *)malloc(1*sizeof(struct student_record_node)); 

在这里,我们铸造malloc返回值来“指向struct节点”,然后将其分配给一个“指向struct student_record_node。”

其实演员是不良作风。转储它。的malloc返回值是void*,这与任何其他指针兼容。此外,您还可以使用位置作为sizeof的说法,而这方面具有优势。 1*只是混乱。所以你留下了:

struct student_record_node *sr = malloc(sizeof *sr); 

和错误将消失。

然后检查空返回值。即使这是一个玩具,也要尽早进入真正的程序员的习惯。

也不是学习c89语法的最好主意,特别是作为一个新的程序员。它在非平凡的情况下实际上需要糟糕的风格。为什么你认为gcc需要一个标志来强制它?我知道这可能无法控制,但我会和老师一起提出。

“面对问题”不是问题。如果你要求特别的帮助,你通常会在这里得到它。

+0

注意:这是一个错误,但也有其他几个 –

0

你不应该强制转换malloc的回报。 See this link as to why should not.

因此,在线174

sr = (struct node *)malloc(1*sizeof(struct student_record_node)); 

修改代码,

sr = malloc(1*sizeof(struct student_record_node)); 

重复此为所有比排序似乎OK三个lines.Other。

要打印到文件,你可以使用函数fprintf(fp,"....")而不是用printf

+0

'printNode()'函数忽略了它的参数并且总是转储整个列表,这并不是真的一个好的设计。应该有一个“打印一个节点”功能,另一个功能是“打印列表中的所有节点”,反过来,该功能会反复调用一次打印一个节点的功能。我怀疑还有其他问题 - 在这个问题的前一版本中肯定存在。 –

+0

在这里不能同意:你的建议会使警告消失,但代码仍会分配错误的字节数。它应该是铸造版本,但是要铸造到正确的类型;或(我的首选)'sr = malloc(sizeof * sr);' –

0

检查此行

if (ptr1 == NULL) 
    return; 

它应该在的地方 “PTR”