c - 试图实现一堆结构

标签 c struct stack

我的作业说:

编写一个程序来执行以下堆栈操作:创建一个包含项目代码和数量的堆栈

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/

相关文章:

c - 从 char 数组中获取最后一个整数值

c - 遍历字符串与打印特定位置时的不同结果 (C)

ios - C 结构与对象

android - LogCat 中的堆栈跟踪

javascript - 使用javascript确定javascript中的堆栈深度

c - OpenGL 存储每个形状的变量,没有任何重复

c - 需要有关以下代码的输出的帮助,并在可能的情况下进行解释

python - 在 Pyrex 中包装相互依赖的结构

c - 如何使这个结构体实例在 C 中可变?

c++ - 在 C++ 中堆叠/连接 2D vector