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 2 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++.
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.
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?
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.
{
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;
}
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.
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.
Gunjan said:
1 decade ago
Actually here we are allocating a memory of 4 bytes 2 for int and 2 for pointer,
But sizeof(p) will give the size of p only which is 2 because p contains the address of that memory location not the memory itself.
But sizeof(p) will give the size of p only which is 2 because p contains the address of that memory location not the memory itself.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers