C Pointers
C Pointers
In this article I will be posting some good c pointer interview questions answers. Pointers in C are special type of variables that are used to point the value of particular data type by storing address of that particular data type. Pointers in c play role in call by reference when we want any change in formal parameter to get reflected in actual parameters. In this post you can find somebasic pointer questions in c.
Function pointers in C
Function Pointers are variables, which point to the address of a function. A running program gets a certain space in the main-memory. Both, the executable compiled program code and the used variables, are put inside this memory. Thus a function in the program code is nothing more than an address. Syntax to create function pointers in c
return type fnPointer* (arguments-list); We can assign address of similar function (with same arguments list, return type) by writing fnPointer=&fnName; // or fnPointer=fnName; Now we can call the function being pointed as fnPointer*(argument-list); Explain the use of generic pointers/void pointer in C. Generic pointers in C are useful when we want same variable to point to different variables at different times. Example c program for generic pointer (source:http://www.faqs.org/docs/learnc/x658.html): main() { int i; char c; void *the_data; i = 6; c = 'a'; the_data = &i; printf("the_data points to the integer value %d\n", *(int*) the_data); the_data = &c; printf("the_data now points to the character %c\n", *(char*) the_data); return 0; } Generic pointers cant be dereferenced directly. We cant write *the_data in above code. We need to first do type casting and then dereference them