Question

Is there anything wrong with this? According to the compiler there is an error in this line

void memory_allocation(struct a *s1,struct b *s2,struct c *s3,int size) 

main looks like

main()
{   
    struct mystack
    {
        int head;
        void **stack;
        void **stackcopy;
        int stack_size;

    } *ptr ; 

    struct professor
    {
        char flag;
        char surname[50];
        char coursbhe[50];
    } *prof;

    struct student
    {
        char flag;
        char surname[50];
        int semester;
    } *stud;

    (*ptr).stack_size=getstacksize();
        memory_allocation(&ptr,&prof,&stud,(*ptr).stack_size);

}
Was it helpful?

Solution

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:

  1. It knows about the types of the structures it is to allocate.
  2. It is passed pointers to the pointers so it can modify the actual pointers in the main().
  3. 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.

OTHER TIPS

Here is the solution.... use your prototype of function as below

void memory_allocation(struct mystack *s1 , struct professor *s2 , struct student *s3 , int d)

because you are calling with defined struct not other and call should be like

memory_allocation(ptr,prof,stud,(*ptr).stack_size);

remove "&" sign or remove *s1 to s1(have to pass address ,not address of address)

please change both things, it will work,my side its working..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top