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.

Shouldn't this have a semi-colon after it?

struct item { int* num ; int* q; };//semi-colon

Why are the structure elements pointers? If you insist on pointers then you'll have to allocate memory for them or point them to valid memory before you use them.

Member Avatar for Mouche

As gerard said, you need a semicolon after the struct declaration. And yes, there's no need for pointers in the declaration. You can pass a pointer to the data for scanf by using '&' in front. That passes the address of a variable. Also, when you change that, you'll want to remove the asterisks in the rest of the program.

Also, on line 21, you set an element of data to NULL. You can't do that because it has to be a struct item. If you want a zeroed struct, you could just set num and q to 0 separately.

Similarly, you can't compare data[i] with NULL since NULL isn't a struct item. Also, you can't compare structs directly. You need to compare the elements inside. If you want to use a comparison in the for loop, you need to write a function that compares two 'struct item's.

As for your approach, shifting the stack each time you see a 0 works, but it can require moving an element a bunch of times. It would be more efficient to only move each element once. I would start from the beginning of the array and step through it, increment a value to know how many good entries you have and move an element if it is not a the earliest open index.

For example, you have: 1 1 2 0 0 0 1 2

Your approach would shift the far right '1 2' three times. Instead, you can do nothing when you see the three zeroes, but know that you have only 3 good entries. So when you get to the next '1' you copy it to the 4th position.

Hope that helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.