Your types are local to the function (main()
) in which they are defined. That means you cannot reliably pass variables of those types to any other function. You should define the types outside of main()
so that other functions can use the same types. There is a concept of type equivalence, but there's also a concept called DRY — Don't Repeat Yourself. The only way another function could use those types is by writing them out again, repeating yourself. Don't!
Your types need to be available outside the function. You don't have to define any global variables when you define the types. For example, you could use:
struct mystack
{
int head;
void **stack;
void **stackcopy;
int stack_size;
};
struct professor
{
char flag;
char surname[50];
char coursbhe[50];
};
struct student
{
char flag;
char surname[50];
int semester;
};
extern int getstacksize(void);
extern void memory_allocation(struct stack **s1, struct professor **s2,
struct student **s3, int size);
extern void memory_release(struct stack *stack, struct professor *prof,
struct student *stud);
int main(void)
{
struct mystack *ptr = 0;
struct professor *prof = 0;
struct student *stud = 0;
int stack_size = getstacksize();
memory_allocation(&ptr, &prof, &stud, stack_size);
memory_release(ptr, prof, stud);
return 0;
}
Note that the memory allocation function cannot do anything useful unless:
- It knows about the types of the structures it is to allocate.
- It is passed pointers to the pointers so it can modify the actual pointers in the
main()
. - The memory release function doesn't need pointer to pointer syntax, though it could be used and if it was used, the memory release function should set the pointed at pointers to NULL when the memory is released to minimize the risk of abusing already freed memory.
The additional variable stack_size
is needed because ptr
doesn't point to anything when getstacksize()
is called, so referencing (*ptr).stack_size
will probably crash and definitely not work correctly. Alternatively, you could change the definition of the variable to stack mystack stack;
and then use stack.stack_size = getstacksize();
and pass stack.stack_size
to the allocation function, but then you don't need the memory allocator to allocate the stack as a whole any more. Separation of duty suggests that there will be a separate function to allocate the stack internals.
All the material from the first struct mystack
line down to but not including int main(void)
is eligible to be put into a header so that the implementation of the functions can see the same type and function definitions as the main()
program can see.