C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 9)
9.
What will be the output of the program in 16-bit platform (under DOS)?
#include<stdio.h>

int main()
{
    struct node
    {
        int data;
        struct node *link;
    };
    struct node *p, *q;
    p = (struct node *) malloc(sizeof(struct node));
    q = (struct node *) malloc(sizeof(struct node));
    printf("%d, %d\n", sizeof(p), sizeof(q));
    return 0;
}
2, 2
8, 8
5, 5
4, 4
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
36 comments Page 1 of 4.

Chandresh said:   2 decades ago
This is because pointer variable always occupy 2 bytes irrespective of their usage.

Sundar said:   2 decades ago
Hi Guys,

If we run this program in DOS (compiled with Turbo C), it will show the output as 2, 2. Because it is a 16-bit platform. If you compile in a 32-bit platform like Linux (compiled with GCC compiler), it will show 4, 4 as the output.

The online compiler given in this websites is a 32 bit (Linux) platform. Show it will show the output as 4, 4.

Hope you understand better. Have a nice day.!

Rohit Kshirsagar-Solapur said:   1 decade ago
The Pointer always have 2 byte if they have integer,.char, float pointer.

int *a
cahr *b
float *c

a,b,c are same size of byte b'coz it store only address of variable if you have TURBOC3 it will bo 2 and you have VC++ then it will give 4.

Mohansai said:   1 decade ago
The size of int is 4 bytes and the struct node *link; is also 4 bytes.

Now the size of the structure is 8 bytes so when you are allocating the memory the size if p & q is 8, 8 in linux gcc compiler I am compiling and telling to you.

Hiriyo said:   1 decade ago
I am with you mohansai you are right.

NRV said:   1 decade ago
The Value Depends on the Compiler .. If Itz running on 16 bit Compiler the value would be 2,2 (Because the pointer would always occupy 2 bytes)..

Satyaprakash said:   1 decade ago
Thank you for Sundar.

Kannan said:   1 decade ago
Actually it is depends on the compiler. Here the sizeof(p) returns the SIZE OF ADDRESS of the pointer. Usually for pointer variable (whatever the datatype maybe) the sizeof operator returns the size of the address part of the pointer variable.

Ex:
Run the same prog in,
>> Turbo C on MS-DOS. The output will be, 2,2.
>> GCC on Linux. The output will be, 4, 4.
etc.. .. for different compilers.

Please check it..!

Abhinay said:   1 decade ago
How much space will struct node will occupy?

John said:   1 decade ago
Thanks everyone for your explanations,

First I was confused sizeof this struck is 4, but actually this is sizeof pointer
sizeof pointer depend on system with 16/32/64 bit

16 bit : is 2 byte
32 bit : 4 byte
64 bit : 8 byte

Thanks
(1)


Post your comments here:

Your comments will be displayed after verification.