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.

Rupa said:   7 years ago
It gives the output as 8, 8 in Ubuntu. Which will be the correct answer.
(1)

Amandeep Singh said:   7 years ago
As per me, the answer is 8, 8.

Shridhar said:   6 years ago
As structure has a size of 2 (i.e sizeof(int) as pointer doesn't have size).

Karthi said:   5 years ago
It will be 4, 4 since int takes 2 bytes and pointer in 16-bit takes 2 bytes since structure 2+2=4. Therefore answer is 4, 4.

Saha said:   4 years ago
The size of a pointer in C/C++ is not fixed. It depends upon different issues like Operating system, CPU architecture etc. Usually it depends upon the word size of underlying processor for example for a 32 bit computer the pointer size can be 4 bytes for a 64 bit computer the pointer size can be 8 bytes. So for a specific architecture pointer size will be fixed.

It is common to all data types like int *, float * etc.

Anjali Patel said:   1 year ago
sizeof(p), sizeof(q)

Here, we are printing the size of a pointer variable, so it will be [2] in a 16-bit system, and [4] in a 32-bit system.

Because the pointer variable just stores the memory address.
(2)


Post your comments here:

Your comments will be displayed after verification.