我的作业说:
编写一个程序来执行以下堆栈操作:创建一个包含项目代码和数量的堆栈
Itemcode Quantity
111 450
112 0
113 487
114 101
115 500
116 0
117 359
然后删除数量为零的项目并更新堆栈。
我的代码是:
#include<stdio.h>
#define LEN 7
struct item { int* num ; int* q; }
int main(void){
int length = LEN,i,j;
struct item data[LEN];
// read input data into struct
for(i=0 ; i < LEN ; i++){
printf(" %d . enter item-code : ",i);
scanf("%d",data[i].num);
printf(" %d . enter quantity : ",i);
scanf("%d",data[i].num);
}
// Delete the items having quantity zero and update the stack
for(i=0 ; i < length ; i++) if(*data[i].q == 0){
for(j=i+1;j<length;j++)
data[j-1] = data[j]; // update by overwriting
data[j] = NULL;
length--;
}
// display stack
for(i=0 ; i < length && data[i]!= NULL; i++){
printf(" %d > item : %d , quantity : %d\n",i,*data[i].num,*data[i].q);
}
return 0;
}
这是我第一次在 C 中使用结构。我得到的错误是:
StructStack.c:5: error: two or more data types in declaration specifiers
StructStack.c: In function 'main':
StructStack.c:21: error: incompatible types when assigning to type 'struct item' from type 'void *'
StructStack.c:26: error: invalid operands to binary != (have 'struct item' and 'void *')
StructStack.c:30: error: incompatible types when returning type 'int' but 'struct item' was expected
任何帮助都会很棒。
问候 顺吉。
最佳答案
我最好的猜测:结构后缺少分号。
在用 C 编程时,首先要看的是这类东西。
关于c - 试图实现一堆结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20574763/