Home
Questions
Search
Forum
Contact
Guest Book
Polls!
Got a Question?
 
PREV
Programs
NEXT
 
(54 / 301)
 
 



How do you initialize a pointer inside a function?




This is one of the very popular interview questions, so take a good look at it!.


myfunction(int *ptr)
{
  int myvar = 100;
  ptr = &myvar;
}

main()
{
 int *myptr;
 myfunction(myptr);

 //Use pointer myptr.

}


Do you think this works? It does not!.


Arguments in C are passed by value. The called function changed the passed copy of the pointer, and not the actual pointer.


There are two ways around this problem

Method1

Pass in the address of the pointer to the function (the function needs to accept a pointer-to-a-pointer).


calling_function()
{
  char *string;
  return_string(/* Pass the address of the pointer */&string);
  printf(?\n[%s]\n?, string);
}

boolean return_string(char **mode_string /*Pointer to a pointer! */)
{
  *string = (char *) malloc(100 * sizeof(char)); // Allocate memory to the pointer passed, not its copy.
  DISCARD strcpy((char *)*string, (char *)?Something?);
}



Method2

Make the function return the pointer.


char *myfunc()
{
  char *temp = "string";
  return temp;
}

int main()
{
  puts(myfunc());
}



PREV
COMMENTS                                  INDEX                                  PRINT
NEXT



Last updated: November 3, 2005

www.cracktheinterview.com - Your destination for the most common IT interview questions, answers, frequently asked interview questions (FAQ), C Programs, C Datastructures for technical interviews conducted by the top IT companies around the world!