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

how many key words availabel in c a) 28 b) 31 c) 32

629


What is the difference between formatted&unformatted i/o functions?

610


How does placing some code lines between the comment symbol help in debugging the code?

538


What is the purpose of void in c?

615


What is structure in c definition?

568






Explain low-order bytes.

617


What will the preprocessor do for a program?

584


Explain how can I convert a number to a string?

636


The performance of an operation in several steps with each step using the output of the preceding step a) recursion b) search c) call by value d) call by reference

669


hw can we delete an internal node of binary search tree the internal node has child node..plz write progarm

1629


What is a static function in c?

615


write a program to find out prime number using sieve case?

1631


Why use int main instead of void main?

588


How are variables declared in c?

596


What are the features of the c language?

639