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 4 of 4.

Soumikdaschoudhury said:   1 decade ago
Guys clear me one thing...

The structure is having an int and an pointer. So its size should be (2+2) =4 byte fr 16 bit compilers. So what you say?

Anam.... said:   1 decade ago
Hey. Can anybody please clear me concept of structure padding?

Sri said:   1 decade ago
I think the answer is 2, 2 because struct node is containing only one variable of type int that means the size of struct node is 2 which is initialized to both p and q.

Neha said:   1 decade ago
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;
}

Now I guess you can understand better.

Yogeshwar Singh said:   1 decade ago
#include<stdio.h>
int main()
{
char *p;
printf("%d\n", sizeof(p)); //Prints the size of pointer.
printf("%d",sizeof(*p)); //Prints the size of datatype pointed by pointer.
return 0;
}

Manideep innamuri said:   1 decade ago
As pointer occupies 2 bytes in 16 bit platform the size will be returned as 2.


Post your comments here:

Your comments will be displayed after verification.