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.
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.
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;
}
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..!
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.
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;
}
Sundar said:
2 decades ago
Hi Guys,
If we run this program in DOS (compiled with Turbo C), it will show the output as 2, 2. Because it is a 16-bit platform. If you compile in a 32-bit platform like Linux (compiled with GCC compiler), it will show 4, 4 as the output.
The online compiler given in this websites is a 32 bit (Linux) platform. Show it will show the output as 4, 4.
Hope you understand better. Have a nice day.!
If we run this program in DOS (compiled with Turbo C), it will show the output as 2, 2. Because it is a 16-bit platform. If you compile in a 32-bit platform like Linux (compiled with GCC compiler), it will show 4, 4 as the output.
The online compiler given in this websites is a 32 bit (Linux) platform. Show it will show the output as 4, 4.
Hope you understand better. Have a nice day.!
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.
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?
Rohit Kshirsagar-Solapur said:
1 decade ago
The Pointer always have 2 byte if they have integer,.char, float pointer.
int *a
cahr *b
float *c
a,b,c are same size of byte b'coz it store only address of variable if you have TURBOC3 it will bo 2 and you have VC++ then it will give 4.
int *a
cahr *b
float *c
a,b,c are same size of byte b'coz it store only address of variable if you have TURBOC3 it will bo 2 and you have VC++ then it will give 4.
Mohansai said:
1 decade ago
The size of int is 4 bytes and the struct node *link; is also 4 bytes.
Now the size of the structure is 8 bytes so when you are allocating the memory the size if p & q is 8, 8 in linux gcc compiler I am compiling and telling to you.
Now the size of the structure is 8 bytes so when you are allocating the memory the size if p & q is 8, 8 in linux gcc compiler I am compiling and telling to you.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers