0

I'm trying to write some stack implementation code here and I'm hung up on a few compiling errors/looking for some clarification on a few of the issues I'm seeing.

In particular I'm having difficults figuring out what the input should be in the main function for my functions to run. They expect pointers as inputs, how do I give them pointers?

My isEmpty() and my push() don't like my inputs. :(

Here is my code--

#include <stdio.h>
#include <conio.h>
#define MAX 100
typedef struct {
int * st; // array of the data on the stack
int size;
int T;
int x;
int a[];
} stack;



void push(stack *st, int item) {
    printf("\nT value%d",st->T);
    st->T++;
    if (st->T >= st->size) {
        printf("\nStack size limit reached, can't push.");
        return;
    }
    st->T++;
    printf("\nT value%d",st->T);
    st->a[st->T]=item;
    printf("\n array value at position T %d", st->a[st->T]);
}

int pop(stack *st) {
    int item;
    printf("\nT value%d", st->T);   
    item=st->a[st->T];
    printf("\n item= %d", item);
    st->T--;
    printf("\nT value%d", st->T);
    return(item);

}

int size(stack *st){
    int size_of_stack=0;
    size_of_stack = st->T + 1; 
    printf ("\n size of stack = %d", size_of_stack);
    return size_of_stack;
}

int isEmpty(stack *st)
{
  if(st->T == -1) 
     return(1);
  else
     return(0);
}

int top(stack *st, stack T){
    int value= st->T;
    return(value);
}

void print(stack *st){
    int i;
    if (isEmpty(*st)==1)
        printf("\n Stack is empty! \n");
    else {
        for (i=st->T; i>= 0 ; i-- )
            printf ("\n%d",st->a[i]);
    }
}

int main(){
    int st[MAX],item;
    int T=-1;
    int a[6]={10,20,30,40,50,60};
    push(* st,2);

}

Here are the compile errors that I'm getting for this.

λ gcc a3.c
a3.c: In function 'print':
a3.c:60:6: error: incompatible type for argument 1 of 'isEmpty'
  if (isEmpty(*st)==1)
      ^
a3.c:45:5: note: expected 'struct stack *' but argument is of type 'stack'
 int isEmpty(stack *st)
     ^
a3.c: In function 'main':
a3.c:72:7: warning: passing argument 1 of 'push' makes pointer from integer without a cast
  push(* st,2);
       ^
a3.c:14:6: note: expected 'struct stack *' but argument is of type 'int'
 void push(stack *st, int item) {
2
  • Can you post the compiler errors as well? It'll help pinpoint the cause of at least some of your issues Commented Mar 21, 2016 at 2:21
  • Yes of course, one sec Commented Mar 21, 2016 at 2:26

1 Answer 1

2

There are quite a few issues here.

  1. You're attempting to push to something that is not a stack. When you call push(*st, 2) the function is receiving an int instead of a stack. This is because *st is attempting to get the value stored at address st, which in this case is st[0].

  2. Even if you weren't taking the value at st, you never actually create a stack. You're just making an array. You need to actually create a variable of type stack and initialize it.

  3. The stack type has two arrays, one of which (st) is never referenced.

  4. Your push function increments T twice for no apparent reason. I assume T is the index into the array.

  5. Last but certainly not least, it's virtually impossible to tell what any of the fields of stack are for. Why does stack have a size and not reference it in the size function? What are T, x, and a? You should try to give these more meaningful names, it's very hard to debug a program that can't be read.

Also some style things:

  1. return is not a function, it is a keyword. The value being returned is generally not enclosed in parentheses.

  2. Newlines generally go at the end of a string, not at the beginning.

Sign up to request clarification or add additional context in comments.

4 Comments

So something like stack new; in the main function would solve #2? What do I initialize it to then? I feel like I can't leave it like that. Then shoving new as an input in the functions in main should fix #1 then
You're correct, you need to give it a proper initializer. stack new = { <values> };, and give a value for each element of new.
I'd imagine push() would populate new so I can just initialize new to { }. And since I'm using a pointer I think inputting push(&new,2) is the correct way to input using pointers with these stacks.
Thanks cormac-obrien! It compiled ^_^ now to sort through its other kinks. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.