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.
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.
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?
Shridhar said:
6 years ago
As structure has a size of 2 (i.e sizeof(int) as pointer doesn't have size).
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.
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;
}
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.
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.
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++.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers