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 1 of 4.
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)
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.
It is common to all data types like int *, float * etc.
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.
Shridhar said:
6 years ago
As structure has a size of 2 (i.e sizeof(int) as pointer doesn't have size).
Amandeep Singh said:
7 years ago
As per me, the answer is 8, 8.
Rupa said:
7 years ago
It gives the output as 8, 8 in Ubuntu. Which will be the correct answer.
(1)
Ashish said:
8 years ago
Yes, Agree @Hardik.
It's 8,8 for 64 bit.
It's 8,8 for 64 bit.
Hardik chugh said:
8 years ago
For 64-bit linux os answer comes to be 8, 8.
Hitesh said:
8 years ago
@ALL.
The modified program to understand it better.
int main()
{
int *d;
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)); // p and q both are pointers
printf("\nsize of struct node is %d ", sizeof(struct node)); // size =integer + pointer
printf("\nsize of any pointer is %d ", sizeof(d));
return 0;
}
The modified program to understand it better.
int main()
{
int *d;
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)); // p and q both are pointers
printf("\nsize of struct node is %d ", sizeof(struct node)); // size =integer + pointer
printf("\nsize of any pointer is %d ", sizeof(d));
return 0;
}
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers