【发布时间】:2013-12-23 13:19:16
【问题描述】:
我正在初始化一个名为 Array 的结构,它由一个 Items 数组和一个用于跟踪 Items 数量的 int 组成。
typedef struct anything{
char text[MAXI];
int any;
}Item;
typedef struct array{
Item arr[0];
int size;
}Array;
为了初始化数组,我使用 malloc 并返回指针。
Array create(){
Array *p1 = malloc(sizeof(Array));
p1->size = 0;
return *p1;
}
我的问题是有些函数是为2个数组设计的,比如交集:
Array intersection(Array S,Array T){
int i, j;
Array I = create();
for(i=0; i<(S.size); i++){
for(j=0; j<(T.size); j++){
if((compStructs(S.arr[i], T.arr[j])) == true)
add(I, S.arr[i]);
}
}
return I;
}
根据这段代码,如果我执行 create() 两次,我会丢失指向第一个数组的指针。我可以在主程序中做些什么来防止这种情况发生,或者我可以对当前功能做哪些修改?
编辑:添加添加项目功能:
void add(Array S,Item x){
bool rep = false;
int i = 0;
for(i = 0; i<(S.size); i++){
if(compStructs(x,S.arr[i]) == true)
rep = true;
}
if(rep == false){
Item *p2 = realloc(S.arr, sizeof(Item));
*p2 = x;
(S.size)++;
}
else
printf("The item is already in the set.");
}
我的主要问题是我不确定如何调用两个要根据用户请求创建的单独数组。该程序正在构建中,但目前它看起来像这样:
#include "array.h"
Item x;
void arraymenu(){
char inp;
while((inp = getchar())!= '\n'){
printf("a. To Create a new Array\n"
"b. To Add a new Item\n"
"c. To Remove an Item\n"
"d. To Clear all contents of the Array\n"
"e. To Get the size of the Array\n"
"f. To Get a list of the number of elements of your choice\n"
"g. To Check whether the array is empty\n"
"h. To Get the union of two sets\n"
"i. To Get the intersection of two sets\n"
"j. To Get the difference of two sets\n"
"k. To Check if a set is the subset of the other one\n"
"l. To map a function to all Items of an Array\n"
"m. To apply a function to all Items of an Array\n"
"n. To store the Array in a File\n"
"o. To load the Array from a File\n" );
switch(inp){
case 'a' : printf("Array A has been created.");
Array A = create();
break;
case 'b' : printf("Enter any integer, followed by any string.");
scanf("%d", &x.any);
scanf("%s", &x.text);
add(A, x);
break;
case 'c' : printf("Enter the integer and string you wish to remove ");
scanf("%d", &x.any);
scanf("%s", &x.text);
removee(A,x);
break;
}
}
}
【问题讨论】:
-
为什么数组后面有大小字段?而且你不想用更大的尺寸 malloc 它,而不是一个有足够空间容纳零元素的数组吗?
-
size 字段用于知道 Item arr[] 的大小。我正在使用带有 0 个元素的 malloc,因为我在另一个函数中使用 realloc 来添加项目。如果需要,将对其进行编辑。
-
不想听起来傲慢,您可能想了解 pointers 与 arrays
Item arr[0]的区别(和要点)真的没有意义。另外:malloc包装函数应该,IMO 返回malloc返回的内容:指针 -
每次调用
add时,您都会将arr重新分配给sizeof(Item)...1 项...如果您想要更多项,则必须将大小乘以项数跨度> -
@user2035045:你不明白:你没有添加任何东西看到我的回答的结尾:realloc需要指定新的总大小 i> 你需要:
realloc(void *, (current_size + add_size) * sizeof(type));
标签: c arrays data-structures struct