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.
Parthasarathi Nayak said:
1 decade ago
#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;
}
As we know in GCC compiler integer takes 4 byte, but when we are finding out the sizeof structure internally structure padding occur and even though in structure only one member is integer type here in program another member is structure pointer it will not be accounted as when cause its structure type, it has a special function only holds the address not allocate any memory so that when we are finding out the sizeof structure it will give the first member size in this program.
Ans for GCC -p=4, p=4, better to try and compile by own you will understand.
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;
}
As we know in GCC compiler integer takes 4 byte, but when we are finding out the sizeof structure internally structure padding occur and even though in structure only one member is integer type here in program another member is structure pointer it will not be accounted as when cause its structure type, it has a special function only holds the address not allocate any memory so that when we are finding out the sizeof structure it will give the first member size in this program.
Ans for GCC -p=4, p=4, better to try and compile by own you will understand.
Giriraj VIT Chennai said:
1 decade ago
For better understanding. Try this:
#include<stdio.h>
#include<string.h>
int main()
{
struct node
{
int data;
//struct node *link;
//Here size of embedded pointer doesn't matter in sizeof(strucutre)
};
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;
}
#include<stdio.h>
#include<string.h>
int main()
{
struct node
{
int data;
//struct node *link;
//Here size of embedded pointer doesn't matter in sizeof(strucutre)
};
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;
}
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)
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)
RAJI said:
9 years ago
Thank you so much @Chandresh.
(1)
Ramya said:
9 years ago
The program prints the sizeof(p) &sizeof(q),then wat is the use of this two.
p = (struct node *) malloc(sizeof(struct node));
q = (struct node *) malloc(sizeof(struct node));
Without this two the program will give the output, then what is the need to this two lines?
p = (struct node *) malloc(sizeof(struct node));
q = (struct node *) malloc(sizeof(struct node));
Without this two the program will give the output, then what is the need to this two lines?
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.
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;
}
Hardik chugh said:
8 years ago
For 64-bit linux os answer comes to be 8, 8.
Ashish said:
8 years ago
Yes, Agree @Hardik.
It's 8,8 for 64 bit.
It's 8,8 for 64 bit.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers