Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Return from void function to another void function

void reserve(int x)
{
    stream = begin;
    if (begin == NULL)
    {
        begin = stream = (struct room*)malloc(sizeof(struct room));
        details();
        stream->following = NULL;
        printf("\n\t room booking is successful!");

    
        x = stream->room_period;
        printf("\n\t your room period is: period-%d", x);
        stream->room_period = x;
        
        return;
        
    }

After running details(), will the program automatically continue to run or some words need to be added ??

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If the function detals() terminate (returns) then the following line in reserve() will run which in this case is:

     stream->following = NULL;

details presumably sets the global variable stream. Otherwise it’s undefined behavior as malloc() does not initialize the memory being allocated. I suggest you avoid global variables if possible and instead pass them those variables as arguments:

details(&stream);

In C we don’t cast void * so it should just be, and I suggest you use the variable rather than the type as argument to sizeof:

        begin = stream = malloc(sizeof *begin);

You should check the return value from malloc(). It will return
NULL on failure and this will cause the stream->following to segfault.

Not sure why you read a variable then write it back again to the same place. As x is an argument whatever you assign to it will be discard when the function returns. At least in this sample x isn’t serving any purpose and could just be eliminated.

        x = stream->room_period;
        printf("\n\t your room period is: period-%d", x);
        stream->room_period = x;

Maybe you just want to do this instead?

        printf("\n\t your room period is: period-%d", stream->room_period);

Your function is missing a } so this will not compile as is.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading