1

在这个程序中,我想将元素放在列表中而不是数组中。最后打印出清单。

例如。西蒙 22 苏西 24
...

但是,我并不真正了解如何操作列表,以及如何构建堆并检索它。我做了一些关于如何做到这一点的研究。这就是我想出的。并且出现了一些错误,我不知道如何解决。

error: 'ptr' undeclared (first use in this function)
arrays.c:37:5: note: each undeclared identifier is reported only once for each function     it appears in
arrays.c: In function 'main':
arrays.c:62:9: error: expected identifier or '(' before '=' token
arrays.c:69:5: warning: passing argument 1 of 'insert' from incompatible pointer type
arrays.c:28:13: note: expected 'struct Record *' but argument is of type 'struct Record **'

#include <stdio.h>
#include <stdlib.h>


/* these arrays are just used to give the parameters to 'insert',
   to create the 'people' array */
char *names[7]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim",
          "Harriet"};
int ages[7]= {22, 24, 106, 6, 18, 32, 24};


/* declare your struct for a person here */
/* */
typedef struct Record{
    char *names;
    int ages;
    struct Record *next;
}  Record;

char getname(Record *names){
    return names;
}

int getage(Record *ages){
    return ages;  
}

static void insert (Record *p, char *s, int n) {

//p[(*)] = malloc(sizeof(person));

/*static int nextfreeplace = 0;*/
    Record *headptr = NULL;

    while(!reached_eof(p)){

/* allocate heap space for a record */
        ptr =(Record*) malloc(sizeof(Record));

        if(ptr == NULL){  
            abort();
            printf("memory allocation fail"); 
            exit(1);  
        }
        else{
            printf("memory allocation to person  - %s - \n", s);      
        }

        ptr->name = getname(p);
        ptr->age = getage(p);

        /* link new object into the list */
        ptr->next = headptr;
        headptr = ptr;

    }
}       


int main( int argc, char **argv) {

    /* declare nextinsert */
    int  = 0;            

    /* declare the people array here */
    Record *p, *headptr;
    headptr = NULL;

    //insert the members and age into the unusage array. 
    for ( int i=0 ; i < 7; i++) {
        insert (p, names[i], ages[i]);

        /* do not dereference the pointer */
    }

    /* print out a line before printing the names and ages */
     printf("\n");


    /* print the people array here*/
    for (int i=0; i < 7; i++) {
        printf("The name is: %s, the age is:%i\n", p[i]->names, p[i]->ages);
    }


    /* This is the third loop for call free to release the memory allocated by malloc */
    /* the free()function deallocate the space pointed by ptr. */
    for(int i=0; i<7;i++){
        free(p[i]);
    }
}
4

2 回答 2

3

首先

下面的代码很奇怪

char getname(Record *names){
  return names;
}

int getage(Record *ages){
  return ages;  
}

我认为对上述功能没有任何实际需求。

甚至这些台词

ptr->name=getname(p);
ptr->age=getage(p);

您可以将它们替换为

ptr->name=s;
ptr->age=n;

以下函数包含许多错误和奇怪的代码:

static void insert (Record *p, char *s, int n) {

//p[(*)] = malloc(sizeof(person));

/*static int nextfreeplace = 0;*/
   Record *headptr = NULL;

   while(!reached_eof(p)){
/* allocate heap space for a record */
ptr =(Record*) malloc(sizeof(Record));

if(ptr == NULL){  
   abort();
   printf("memory allocation fail"); 
   exit(1);  
}else{
   printf("memory allocation to person  - %s - \n", s);      
}

为什么要使用 while 循环。并且您错过了 ptr 指针的定义,并且您注意到在函数末尾传达了新的标头。在你如何修复它之后:

static void insert (Record **header, char *s, int n) {

   Record *ptr;
   ptr =(Record*) malloc(sizeof(Record));

    if(ptr == NULL){  
       abort();
       printf("memory allocation fail"); 
       exit(1);  
    }else{
       printf("memory allocation to person  - %s - \n", s);      
    }
    ptr->name=s;
    ptr->age=n;

    /* link new object into the list */
    ptr->next=*header;
    *headptr=ptr;
}

在你的主要功能中:

int main(int argc, char **argv) {


   int  i= 0;
  Record *p, *headptr=NULL;

for (int i=0; i < 7; i++) {
insert (&headptr, names[i], ages[i]);
/* do not dereference the pointer */
  }

 for (int i=0; i < 7; i++) { /* this will print from array*/
    printf("From array  The name is: %s, the age is:%i\n", p[i]->names, p[i]->ages);
}

for (p=headptr; p!=NULL; p=p->next) {  /* this will print from linked list*/
    printf("From linked list The name is: %s, the age is:%i\n", p->names, p->ages);
}


}
于 2012-11-25T15:51:15.900 回答
0

我想唯一的错误是你在使用 malloc 函数之前错过了定义指针。将指针定义为

Record *ptr ;

在您的代码中某处有以下文本:

int = 0;

这是第二个错误。

你正在传递一个 NULL 指针p来插入你的 for 循环中的函数

编辑这些,他们现在必须修复错误。

于 2012-11-25T15:50:10.460 回答