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.
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)
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;
}
K.eswar said:
10 years ago
If the question is printf(%d,%d\n",sizeof(struct node));
The answer would be 4.
But he is asking sizeof (p) which is a pointer.
Sizeof (pointer variable) is always 2 only irrespective of whether int* or struct node*.
The answer would be 4.
But he is asking sizeof (p) which is a pointer.
Sizeof (pointer variable) is always 2 only irrespective of whether int* or struct node*.
(1)
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.
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.
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)
Jewel Sengupta said:
8 years ago
The pointer contains only the integer format address value of any type of the variable, it does not depend on the type of data it is storing.
So, the size of the pointer is 2 bytes.
So, the size of the pointer is 2 bytes.
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.
Mayur Gaikwad said:
10 years ago
Hello friends.
The size of pointer depends upon the system with 16/32 bit.
So for 16 bit it will take 2 bytes.
For 32 bit system it will take 4 bytes.
Thank you.
The size of pointer depends upon the system with 16/32 bit.
So for 16 bit it will take 2 bytes.
For 32 bit system it will take 4 bytes.
Thank you.
(1)
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?
NRV said:
1 decade ago
The Value Depends on the Compiler .. If Itz running on 16 bit Compiler the value would be 2,2 (Because the pointer would always occupy 2 bytes)..
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers