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.
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.
{
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.
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.
Anam.... said:
1 decade ago
Hey. Can anybody please clear me concept of structure padding?
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?
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?
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.
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++.
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
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)
Abhinay said:
1 decade ago
How much space will struct node will occupy?
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..!
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..!
Satyaprakash said:
1 decade ago
Thank you for Sundar.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers