C Programming - Memory Allocation - Discussion
Discussion Forum : Memory Allocation - True / False Questions (Q.No. 1)
1.
malloc() returns a float pointer if memory is allocated for storing float's and a double pointer if memory is allocated for storing double's.
Discussion:
16 comments Page 1 of 2.
Bala Aathikesavan said:
2 years ago
I think malloc() always returns the void pointer.
Syntax:
void * malloc(size_t)
int *ptr=(int *)malloc(4)
Here malloc returns the void pointer and we typecasted as an integer here using(int) and it will be stored in a ptr.
Syntax:
void * malloc(size_t)
int *ptr=(int *)malloc(4)
Here malloc returns the void pointer and we typecasted as an integer here using(int) and it will be stored in a ptr.
Amol said:
8 years ago
Thanks @Anil and @Ashwin.
Anushka said:
9 years ago
But I think as address can never be float or double so there is no matter to return of float or double pointer.
Prabin said:
10 years ago
Malloc and calloc always returns void pointers.
DEBENDRA KUMAR SAHOO said:
1 decade ago
Return type depend on typecast like p = (int*)malloc(90).
It will return integer type pointer.
It will return integer type pointer.
Balaji said:
1 decade ago
Can't get your point @Ashwin is that malloc doesn't return float?
M.Salman said:
1 decade ago
malloc and calloc always returns no of bytes allocated which integer.
Ashwin said:
1 decade ago
float *A;
A = (float*) malloc(1000*sizeof(float));
Because malloc is a generic memory allocation function, it is set up to return a generic pointer to a block of memory. Some compilers will complain if we attempt to assign a generic pointer to our pointer variable, so we have to use the type cast syntax (float*) to tell the compiler to reinterpret the generic pointer that malloc returns as a pointer to an array of floats.
A = (float*) malloc(1000*sizeof(float));
Because malloc is a generic memory allocation function, it is set up to return a generic pointer to a block of memory. Some compilers will complain if we attempt to assign a generic pointer to our pointer variable, so we have to use the type cast syntax (float*) to tell the compiler to reinterpret the generic pointer that malloc returns as a pointer to an array of floats.
(2)
Durgam_anil said:
1 decade ago
Prototype of malloc is (void*)malloc(size_t);
i=(void *)malloc(sizeof);//it return void pointer
i=(int*)malloc(sizeof(int));//it return integer pointer
i=(char*)malloc(sizeof(char));//it return char pointer
i=(void *)malloc(sizeof);//it return void pointer
i=(int*)malloc(sizeof(int));//it return integer pointer
i=(char*)malloc(sizeof(char));//it return char pointer
Ashish kumar said:
1 decade ago
malloc always return void pointer it is wrong. malloc return void pointer when there is no typecasting. if there is typecasting then malloc return the pointer of typecast.
as
i=(void *)malloc(sizeof);//it return void pointer
i=(int*)malloc(sizeof(int));//it return integer pointer
as
i=(void *)malloc(sizeof);//it return void pointer
i=(int*)malloc(sizeof(int));//it return integer pointer
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers