2012-03-08 178 views
0

我一直在尝试几个小时才能使该函数正常工作。这里的任务:动态分配的数组结构

Add: Request the part name, price, and quantity. Save the information to a dynamically allocated array of structs. You may allocate space for up to 3 structs at a time. You will need to create more memory dynamically as needed. Use this struct (you may use typedef if you want to):

到目前为止我的代码是

typedef struct { 
    char* name; 
    float price; 
    int quantity; 
}part; 


void add(part *item, int *part_count) 
{ 
    //char temp[100]; 

    if (!item){ 
     item = malloc(sizeof(part)*3); 
    } 
    else{ 
     item = realloc(item, sizeof(part) * ((*part_count*3) + 1)); 
    } 

    item[*part_count].name = malloc(sizeof(char)*64); // max of 64 characters 

    printf("Please enter item name: \n"); 
    //fgets(temp, strlen(temp), stdin); 
    //sscanf(temp, "%s", item[*part_count].name); 
    scanf("%64s", item[*part_count].name); 

    printf("Please enter item price: \n"); 
    //fgets(temp, strlen(temp), stdin); 
    //sscanf(temp, "%f", &item[*part_count].price); 
    scanf("%f", &item[*part_count].price); 

    printf("Please enter item quantity: \n"); 
    //fgets(temp, strlen(temp), stdin); 
    //sscanf(temp, "%d", &item[*part_count].quantity); 
    scanf("%d", &item[*part_count].quantity); 

    *part_count = *part_count+ 1; 
    } 

我曾试图采取与fgets()sscanf()输入,但使用的代码它永远不会允许用户输入数据,然后结束功能。

我相信问题在于我的内存分配,因为当我尝试对数组执行任何操作(例如打印内容)时出现分段错误。

+0

你会得到哪些线路故障? – 2012-03-08 02:13:13

+2

你如何计算'part_count'?如果是元素的数量,则无法访问该元素。所以如果你的数组大小为10,你不能访问array [10]。 – prelic 2012-03-08 02:13:30

+0

我不知道哪条线我得到seg故障,但我有一个单独的打印功能,当它运行时它seg故障。我曾问过,但显然我的打印功能不是问题。 part_count也从0开始,每次调用add时都会增加。 – 2012-03-08 02:20:48

回答

1

推测,第一次调用add()时,项目将为NULL,并且您将执行其初始分配;后续调用realloc,以便该数组是3倍所需的大小(我不认为是你真正想要的)。

但是,匹配项的参数不会因调用add()而改变,因此它保持为NULL,并且每次调用add()都会像初始调用一样操作,为3分配空间结构(当你添加第四个结构时会出现问题)。

您可以使item成为一个**部分,并且在当前使用part的任何位置使用* part,以便保留指针的新值(您将传递* part的地址作为参数)。或者使用item的新值作为函数的返回值,这是一个更清洁的恕我直言。 (这是参考参数派上用场的地方,但C没有这样的东西。)

+0

我得到错误:请求成员'名称'的东西不是结构。 这是我改变了项目结构的类型部分**,然后在函数调用中改变它。 – 2012-03-08 02:51:28

+0

没有看到实际的代码,不能帮上忙。 – 2012-03-08 14:23:12

1

你的函数有一个不可能的接口。它接受一个part *指针。这个指针通过值进入函数。在您分配给它的功能中,从mallocrealloc调用。但是调用者不会看到这个更新的值。当函数返回时,你分配的内存已经泄漏,并且调用者具有原始指针值(可能为null)。另外,最好用动态数组封装动态数组。你有这个“零件数量”变量,它本身就是松散的,它必须与数组一起传递以跟踪其大小。如何包装在一起:

typedef struct part_list { 
    struct part *part; 
    int count; 
} part_list; 

现在有一个函数来初始化空的部分名单。这个必须被所有想要使用其他part_list函数的人调用。

void part_list_init(part_list *pl) 
{ 
    pl->part = 0; 
    pl->count = 0; 
} 

然后编写你的函数来添加零件。

int part_list_add(part_list *pl) 
{ 
    part_list *p; 
    int index = pl->count++; /* increment count, keep old value */ 

    /* realloc accepts a null pointer and then behaves like malloc */ 
    p = realloc(pl->part, sizeof *pl->part * pl->count); 
    if (p == 0) 
    return 0; /* failed to allocate/extend array */ 
    p1->part = p; 

    if ((pl->part[index].name = malloc(64)) == 0) { 
    pl->count = index; /* roll back the count: we didn't really allocate this part */ 
    return 0; 
    } 

    /* your code, updated with pl-> access */ 
    printf("Please enter item name: \n"); 
    scanf("%63s", pl->part[index].name); /* 63s not 64s!!! One byte for NUL char! */ 

    printf("Please enter item price: \n"); 
    scanf("%f", &pl->part[index].price); /* check the return value of scanf!!! */ 

    printf("Please enter item quantity: \n"); 
    scanf("%d", &pl->part[index].quantity); 

    return 1; /* 1 means success */ 
} 
+0

我不能使用链表来解决问题,它看起来像你做了什么。 – 2012-03-08 02:42:22

+0

这里暗示的数据表示不是链表。有一个'part_list'包含一个指向数组的指针。 'part_list'结构封装了数组和大小,使得它们易于作为一个单元传递。 'part_list_add'函数可以通过更新指定结构中的指针来轻松分配和扩展数组。 – Kaz 2012-03-08 03:08:25