i have to impplement a dynamic stack to create a program but when i try to compile my stack.c file(with gcc) I get a bunch of errors here are my files

GLOBALS.h

#ifndef _globals 

#define _globals 



#define DATA( L ) ( ( L ) -> datapointer ) 

#define NEXT( L ) ( ( L ) -> next ) 


typedef enum { OK, ERROR } status ; 
typedef enum { FALSE = 0 , TRUE=1 } bool ;  
typedef void *generic_ptr ; 

#endif

STACK.h

#ifndef _stack
#define _stack

#define STACKINCREMENT 100

#include "globals.h"

typedef struct{ generic_ptr *base; generic_ptr *top; } stack;

extern status init_stack( stack *p_S ) ;

extern bool empty_stack( stack *p_S ); 

extern status push( stack *p_S , generic_ptr data ) ;

extern status pop( stack *p_S , generic_ptr *p_data) ; 

extern status top( stack *p_S , generic_ptr *p_data ) ;

#endif

STACK.c

#include <stdlib.h> 
#include "globals.h"
#include "stack.h"

#define STACKINCREMENT 100 

#define current_stacksize(p_S) ((p_S)->top - (p_S)->base) 


typedef struct{ 
  generic_ptr *base ; 
  generic_ptr *top ; 
  int stacksize ; 
}stack ; 

status init_stack(stack *p_S){

  p_S->base = (generic_ptr *)malloc(STACKINCREMENT*sizeof(generic_ptr)) ; 
  if(p_S->base == NULL) 
    return ERROR ; 

  p_S->top = p_S->base ;
  p_S->stacksize = STACKINCREMENT ;
  return OK ; 
}


bool empty_stack(stack *p_S){ 

  return (p_S->top == p_S->base) ? TRUE : FALSE ; 

}



status push(stack *p_S , genereic_ptr data){ 

  if(current_stacksize(p_S) == p_S->stacksize){
    generic_ptr *newstack = (generic_ptr *) realloc(p_S->base,(p_S->stacksize+STACKINCREMENT)*sizeof(genereic_ptr *)) ; 
    if(newstack == NULL) 
      return ERROR ;  
    p_S->base  = newstack ; 
    p_S->top = p_S->base + p_S->stacksize ;  
    p_S->stacksize += STACKINCREMENT ; 
  }

  *p_S->top = data ; 
  p_S->top++ ; 
  return OK ; 
}


status pop(stack *p_S , generic_ptr *p_data){ 

  if(empty_stack(p_S) == TRUE) 
    return ERROR ; 

  p_S->top-- ; 
  *p_data = *p_S->top ; 
  return OK ;  
} 


status top(stack *p_S,generic_ptr *p_data){

  if(pop(p_S,p_data) == ERROR ) 

    return ERROR ;  

  return push(p_S,*p_data) ; 

}

You should really read your error messages before posting, or post the error messages!

1. There is nothing called genereic_ptr
2. You have defined your stack structure twice

You should not declare your functions as extern if you will provide your own definitions. How do you plan to use this? I assume it is a shared library since it is missing a main function?

@rxlim yes there is in globals.h is defined as a pointer to a void

i plan to use it for a graphical region fill program using integers


errors i get :


gcc -ansi -Wall -pedantic -c stack.c
stack.c:14: error: conflicting types for 'stack'
stack.h:12: error: previous declaration of 'stack' was here
stack.c:16: error: conflicting types for 'init_stack'
stack.h:14: error: previous declaration of 'init_stack' was here
stack.c:16: error: conflicting types for 'init_stack'
stack.h:14: error: previous declaration of 'init_stack' was here
stack.c:28: error: conflicting types for 'empty_stack'
stack.h:16: error: previous declaration of 'empty_stack' was here
stack.c:28: error: conflicting types for 'empty_stack'
stack.h:16: error: previous declaration of 'empty_stack' was here
stack.c:36: error: syntax error before "genereic_ptr"
stack.c: In function `push':
stack.c:36: error: number of arguments doesn't match prototype
stack.h:18: error: prototype declaration
stack.c:38: error: `p_S' undeclared (first use in this function)
stack.c:38: error: (Each undeclared identifier is reported only once
stack.c:38: error: for each function it appears in.)
stack.c:39: error: `genereic_ptr' undeclared (first use in this function)
stack.c:39: error: syntax error before ')' token
stack.c:47: error: `data' undeclared (first use in this function)
stack.c: At top level:
stack.c:53: error: conflicting types for 'pop'
stack.h:20: error: previous declaration of 'pop' was here
stack.c:53: error: conflicting types for 'pop'
stack.h:20: error: previous declaration of 'pop' was here
stack.c:64: error: conflicting types for 'top'
stack.h:22: error: previous declaration of 'top' was here
stack.c:64: error: conflicting types for 'top'
stack.h:22: error: previous declaration of 'top' was here
stack.c: In function `top':
stack.c:70: warning: passing arg 1 of `push' from incompatible pointer type

Edited 5 Years Ago by rockerjhr: n/a

@rxlim yes there is in globals.h is defined as a pointer to a void

i plan to use it for a graphical region fill program using integers


errors i get :


gcc -ansi -Wall -pedantic -c stack.c
stack.c:14: error: conflicting types for 'stack'
stack.h:12: error: previous declaration of 'stack' was here
stack.c:16: error: conflicting types for 'init_stack'
stack.h:14: error: previous declaration of 'init_stack' was here
stack.c:16: error: conflicting types for 'init_stack'
stack.h:14: error: previous declaration of 'init_stack' was here
stack.c:28: error: conflicting types for 'empty_stack'
stack.h:16: error: previous declaration of 'empty_stack' was here
stack.c:28: error: conflicting types for 'empty_stack'
stack.h:16: error: previous declaration of 'empty_stack' was here
stack.c:36: error: syntax error before "genereic_ptr"
stack.c: In function `push':
stack.c:36: error: number of arguments doesn't match prototype
stack.h:18: error: prototype declaration
stack.c:38: error: `p_S' undeclared (first use in this function)
stack.c:38: error: (Each undeclared identifier is reported only once
stack.c:38: error: for each function it appears in.)
stack.c:39: error: `genereic_ptr' undeclared (first use in this function)
stack.c:39: error: syntax error before ')' token
stack.c:47: error: `data' undeclared (first use in this function)
stack.c: At top level:
stack.c:53: error: conflicting types for 'pop'
stack.h:20: error: previous declaration of 'pop' was here
stack.c:53: error: conflicting types for 'pop'
stack.h:20: error: previous declaration of 'pop' was here
stack.c:64: error: conflicting types for 'top'
stack.h:22: error: previous declaration of 'top' was here
stack.c:64: error: conflicting types for 'top'
stack.h:22: error: previous declaration of 'top' was here
stack.c: In function `top':
stack.c:70: warning: passing arg 1 of `push' from incompatible pointer type

I think you just typed to fast, ju wrote genereic_ptr.. what I can see, you defined it as generic_ptr (OBS: not generEic_ptr). You have also defined the struct stack twice, this is not allowed! I assume the second definition is correct since the first one seems to be missing a member.

This article has been dead for over six months. Start a new discussion instead.