pointer_variable=(typecasting
datatype*)malloc(sizeof(datatype));
This is the syntax for malloc?Please explain this,how it
work with an example?
Answer Posted / vignesh1988i
ya, this is the above syntax for malloc function........
ya i will clearly explain ,
let us take a small block of coding , here my aim is to get 'n' numbers and print the 'n' numbers ......
#include<alloc.h>
void main()
{
int n ,*pointer;
clrscr();
printf("enter the number of elements u r going to enter :");
scanf("%d",&n);
pointer=(int *)malloc(n*sizeof(int));
the above statement states that : , this function is requesting the OPERATING SYSTEM to allocate 'n' amount of memory of a data type integer. and since the return format of the malloc function is an address , so we are type casting as (int*)before malloc , and the returned starting address will be stored in the pointer variable (pointer) ..
this 'pointer' will have the starting address of the allocated memory dynamically...
that's all..
for(int i=0;i<n;i++)
{
scanf("%d",(pointer+i));
}
for(i=0;i<n;i++)
printf("%d\n",*(pointer+i));
getch();
}
thank u
| Is This Answer Correct ? | 6 Yes | 1 No |
Post New Answer View All Answers
a single linked list consists of nodes a to z .print the nodes in reverse order from z to a using recursion
When should we use pointers in a c program?
Why is python slower than c?
Explain what is the most efficient way to store flag values?
Find duplicates in a file containing 6 digit number (like uid) in O (n) time.
Find MAXIMUM of three distinct integers using a single C statement
Why static is used in c?
Can we initialize extern variable in c?
application attempts to perform an operation?
Why is c called c?
Which is an example of a structural homology?
For what purpose null pointer used?
What are the c keywords?
Explain data types & how many data types supported by c?
What is 2 d array in c?