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
What are the advantages and disadvantages of c language?
Hai,I have done with my bachelor of commerce and planing to ms,please suggest me how to convince vo for shifting from commerce to computers. Visa on 8 DEC 2014 Npu university
Why do we need functions in c?
Linked lists -- can you tell me how to check whether a linked list is circular?
What are the types of data structures in c?
A routine usually part of the operation system that loads a program into memory prior to execution a) linker b) loader c) preprocessor d) compiler
Under what circumstances does a name clash occur?
Which built-in library function can be used to match a patter from the string?
What is a structural principle?
What is a method in c?
Hai sir, I had planned to write the NIC scientific engineer exam , plz post the sample question......
Explain what is a stream?
.main() { char *p = "hello world!"; p[0] = 'H'; printf("%s",p); }
How can I swap two values without using a temporary?
What is n in c?