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;
}
Discussion:
36 comments Page 3 of 4.
Akash said:
1 decade ago
Hey all. Do I need to write a special header file for malloc. It shows error : call to undefined function 'malloc'.
I'm using borland c++.
I'm using borland c++.
PRITAM GORAIN said:
1 decade ago
The output of this question will be 4, 4 in gcc compiler. Since according to ANSI standard all pointers are of 4 bytes in 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.
Chandresh said:
2 decades ago
This is because pointer variable always occupy 2 bytes irrespective of their usage.
Manideep innamuri said:
1 decade ago
As pointer occupies 2 bytes in 16 bit platform the size will be returned as 2.
Shridhar said:
6 years ago
As structure has a size of 2 (i.e sizeof(int) as pointer doesn't have size).
Rupa said:
8 years ago
It gives the output as 8, 8 in Ubuntu. Which will be the correct answer.
(1)
Anam.... said:
1 decade ago
Hey. Can anybody please clear me concept of structure padding?
Shashank said:
1 decade ago
@Akash.
You should use stdlib.h if you use malloc function.
You should use stdlib.h if you use malloc function.
Hardik chugh said:
8 years ago
For 64-bit linux os answer comes to be 8, 8.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers