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 some basic pointer
questions in c.
C pointers questions and answers subjective type
- What is a pointer?
- How to make a pointer to pointer data type?
- Explain call by reference using pointers in c.
- Explain pointer to functions in c language.
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);
(*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 can’t be dereferenced
directly. We can’t write *the_data in above code. We need to first do type
casting and then dereference them
More interview questions on pointers in c (Multiple
choice type objective questions)
You can also download this post as pdf at
No comments:
Post a Comment