ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   To Refer this Site to Your Friends   Click Here
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C
 
 


 

 
 C interview questions  C Interview Questions
 C++ interview questions  C++ Interview Questions
 VC++ interview questions  VC++ Interview Questions
 Delphi interview questions  Delphi Interview Questions
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions
Question
pointer_variable=(typecasting
datatype*)malloc(sizeof(datatype));
This is the syntax for malloc?Please explain this,how it 
work with an example?
 Question Submitted By :: Manojkumar
I also faced this Question!!     Rank Answer Posted By  
 
  Re: pointer_variable=(typecasting datatype*)malloc(sizeof(datatype)); This is the syntax for malloc?Please explain this,how it work with an example?
Answer
# 1
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 ?    3 Yes 0 No
Vignesh1988i
 
  Re: pointer_variable=(typecasting datatype*)malloc(sizeof(datatype)); This is the syntax for malloc?Please explain this,how it work with an example?
Answer
# 2
I'll add few more lines to answer above just to make it a
bit more understandable.


Malloc allocates consequetive blocks of memory of requested
size.Then it returns the starting address of this block ,
the address returned is of type void , since it is a void
pointer so we need to typecast it to one that we requested
so typecasting is done( This headache has been removed in
C++ by using new as new automatically does this typecasting)
 
Is This Answer Correct ?    0 Yes 0 No
Pramod
 
 
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
the format specified for hexa decimal is a.%d b.%o c.%x d.%u TCS4
why r u join this company? give solid resons. Infosys7
Is main() function predfined or userdefined?  7
main() { int i,j,A; for(A=-1;A<=1;A++) prinf("%d\t",!!A); }  5
How can I call a function, given its name as a string? ABC-Telecom2
how many keywords do C compile? Microsoft2
write a program to insert an element at the specified position in the given array in c language IBM2
Which of the following are valid "include" formats? A)#include and #include[file.h] B)#include (file.h) and #include C)#include [file.h] and #include "file.h" D)#include <file.h> and #include "file.h" Accenture14
what is the benefit of c30  1
#include<stdio.h> int fun(); int i; int main() { while(i) { fun(); main(); } printf("hello \n"); return 0; } int fun() { printf("hi"); } answer is hello.how??wat is tat while(i) mean? Intel4
fn f(x) { if(x<=0) return; else f(x-1)+x; } HCL3
Give the output for the following program. #define STYLE1 char main() { typedef char STYLE2; STYLE1 x; STYLE2 y; clrscr(); x=255; y=255; printf("%d %d\n",x,y); } ADITI1
what is disadvantage of pointer in C Tech-Mahindra5
how to do in place reversal of a linked list(singly or doubly)?  2
what does " calloc" do? Cadence6
WAP to accept rollno,course name & marks of a student & display grade if total marks is above 200?  2
Program to trim a given character from a string. NetApp4
4.weight conversion: Write a program that will read weight in pounds and convert it into grams.print both the original weight and the converted value.There are 454 grams in a pound.design and carry out a test plan for this program. Wipro1
plz answer.... write a program that reads line (using getline) e.g."345", converts each line to an integer using "atoi" and computes the average of all the numbers read. also compute the standard deviation.  1
Write a programme to find even numbers without using any conditional statement? Infosys3
 
For more C Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com