Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

a single linked list consists of nodes a to z .print the nodes in reverse order from z to a using recursion

2714


When should we use pointers in a c program?

1081


Why is python slower than c?

1023


Explain what is the most efficient way to store flag values?

1173


Find duplicates in a file containing 6 digit number (like uid) in O (n) time.

3183


Find MAXIMUM of three distinct integers using a single C statement

1013


Why static is used in c?

1034


Can we initialize extern variable in c?

1075


application attempts to perform an operation?

1916


Why is c called c?

956


Which is an example of a structural homology?

1246


For what purpose null pointer used?

1011


What are the c keywords?

1146


Explain data types & how many data types supported by c?

1024


What is 2 d array in c?

959