C Programming - Memory Allocation - Discussion
Discussion Forum : Memory Allocation - Find Output of Program (Q.No. 4)
4.
What will be the output of the program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
union test
{
int i;
float f;
char c;
};
union test *t;
t = (union test *)malloc(sizeof(union test));
t->f = 10.10f;
printf("%f", t->f);
return 0;
}
Discussion:
22 comments Page 2 of 3.
Lakshmi prasanna said:
1 decade ago
I can't understand this, please explain from staring any one.
Baru said:
1 decade ago
Explanation please.
Sweety said:
1 decade ago
What does f in 10.10f imply?
Don said:
1 decade ago
free(t) is missing.
Vijay said:
1 decade ago
Is it possible to declare, define union in main() ?
The Guide said:
1 decade ago
malloc() returns pointer to void. That is, pointer to the first memory location of the allocated memory block. The pointer type is unknown at this point, hence typecasting is done later.
Raju Naidu said:
1 decade ago
@priya
t=(union test*)malloc(sizeof(union test));
Here malloc return different type that's y we've to do type conversion to original type..i.e union test*
Ex int i =5 valid here we r assigning the same type value so int type accepted
but int i=0.23 invalid,here we r assigning the float value it cannot possible so here typecasting is need.
int i=(int)0.23
t=(union test*)malloc(sizeof(union test));
Here malloc return different type that's y we've to do type conversion to original type..i.e union test*
Ex int i =5 valid here we r assigning the same type value so int type accepted
but int i=0.23 invalid,here we r assigning the float value it cannot possible so here typecasting is need.
int i=(int)0.23
Nirbhay singh said:
1 decade ago
'union test' is name of user define data type sothat sizeof() operator gives size of varriable used in that datatype and we know that malloc()takes integer argument which tells how many blocks will be created. (union test *) is simple type-casting which will say to compiler that created address is of 'union set' type.
Priya said:
1 decade ago
What is the meaning of :
t = (union test *)malloc(sizeof(union test));
t = (union test *)malloc(sizeof(union test));
Harsha said:
1 decade ago
Thanks Apple.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers