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 :
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.