I've got an assignment where it's been suggested to use data hiding, primarily to use one header file instead of two. I've been following a textbook we've been provided and the compiler still can't find the size of the struct. If the fact that this is an assignment is holding you back you should know that this is not part of my grade, and that I want to know how to actually use data hiding for future reference.
Below is the fixed length array implementation of the stack, after I have edited the header file to include struct definitions, preprocessor definitions, and any structs the implementation is aggregated of:
#include <assert.h>
#include "stack.h"
#define CAP 100
struct stack
{
ELEM arr[CAP] ;
int size ; // # number of elements (size of stack)
int cap ; // capacity
} ;
void init( stack* s )
{
s->size = 0 ;
s->cap = CAP ;
}
_Bool isEmpty( const stack* s )
{
return s->size == 0 ;
}
// ... yadda yadda functionality implementation
The following code is the linked list implementation with the same changes made:
#include <assert.h>
#include <stdlib.h>
#include "stack.h"
typedef struct sNode sNode ;
struct sNode
{
ELEM data ;
sNode* next = NULL ;
} ;
struct stack
{
sNode* head ;
} ;
void init( stack* s )
{
s->head->next = NULL ;
return ;
}
// ... yadda yadda more functionality, not struct definitions
And now here is the header file after moving the definitions to the source files:
#ifndef __KS_STACK_H_
#define __KS_STACK_H_
typedef int ELEM ;
typedef struct stack stack ;
// Initialises a new stack
// Must be called before using
void init( stack* s ) ;
// return true of stack is empty
_Bool isEmpty( const stack* s ) ;
// Inserts i on top of stack
// Stack must have space
void push( stack* s, ELEM i ) ;
// Removes item, returns it
// Stack can't be empty
ELEM pop( stack* s ) ;
// Returns item at top of stack
// Does NOT modify the stack
// Stack can't be empty
ELEM top( const stack* s ) ;
#endif // __KS_STACK_H_
I am aware that many people have had similar issues, and yet the threads I've found yet have not been of use. Please do not just redirect me to a thread that may be a similar issue, but still not the same one. That does not tell me what's wrong in the context of this problem, which is actually what I'm interested in.
My question is regarding to why can the compiler not find the size of the stack implementation after attempting to use data hiding?
Thanks to Barmar for clarifying how information hiding is implemented to me.