Somjit
Somjit

Reputation: 2772

trying to Implement a stack of structs

My assignment says :

Write a program to perform following stack operations : Create a stack with item code and quantity

Itemcode    Quantity
    111     450
    112     0
    113     487
    114     101
    115     500
    116     0
    117     359

and then Delete the items having quantity zero and update the stack.

My code is :

#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;
}

Its the 1st time i'm working with structs in C . The errors i get are :

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

Any help would be great.

regards Somjit.

Upvotes: 0

Views: 774

Answers (4)

varevarao
varevarao

Reputation: 2186

It's C. You can't do:

int a = <something>, another_variable;

You have to put the declaration and assignment into different lines. So,

int a, another_variable;
a = <something>;

Also, as the others mentioned, the ";" after the struct declaration, and the & before each variable in the scanf()

Upvotes: 0

rohit89
rohit89

Reputation: 5773

You've got a few syntax errors in your code.

For the struct definition, aside from the no semi-colon at the end, why are you using pointers?

struct item { int* num ; int* q; } becomes struct item { int num; int q; };

scanf takes address of the variable, so scanf("%d",data[i].num) becomes
scanf("%d", &data[i].num)

You don't need data[j] = NULL either. You have length to keep track.

You also don't need any * in front of your data variable because you're not dealing with pointers.

Upvotes: 1

Bill West
Bill West

Reputation: 41

Missing semicolon after the struct, but also the set/comparison to NULL is done on the struct instead of the members. Instead of:

data[j] = NULL;

you might instead use:

data[j].num = NULL; data[j].q = NULL;

Upvotes: 1

Nahuel Prieto
Nahuel Prieto

Reputation: 371

My best guess: the missing semicolon after the struct.

When programming on C, that kind of things are the first you have to take a look at.

Upvotes: 1

Related Questions