Interview Questions on Pointer

Jan 10 • Resources • 12882 Views • 1 Comment on Interview Questions on Pointer

Q1. What is a Pointer ?
Ans. A pointer is a variable that stores the address of another variable in C language. It is basically a data type defined by the user to create special variables to hold the address of the main variables like int, float, char etc. and also the address of some user defined data types e.g. functions  or that of derived data types e.g. structure, array etc. Pointers are basically used to point a variable and is so commonly understandable by its name. Let’s make our point more clear, look the following image-

Pointers interview Questions and answers

Concept of Pointers in C

The syntax for writing a pointer in C language is as follows-
<Pointer Type> <*Pointer Name>

A C Language Program as an example for understanding a Pointer is given below-
#include <stdio.h>
int main(void)
{
char ch = ‘c’;
char *chptr = &ch;
int i = 20;
int *intptr = &i;
float f = 1.20000;
float *fptr = &f;
char *ptr = “I am a string”;
printf(“\n [%c], [%d], [%f], [%c], [%s]\n”, *chptr, *intptr, *fptr, *ptr, ptr);
return 0;
}

Q2. What is (void*)0 ?
Ans. Its a representation of the NULL pointer. It point the value of a pointer is zero. This NULL Pointer is used to hide any information in C language program. Using this will prevent the compiler from dereferencing. A program to show the use of this NULL Pointer is given below-

void abc(void *a, int b)
{
if(b==1)
printf(“%d”,*(int*)a);                // If an integer pointer received
else if(b==2)
printf(“%c”,*(char*)a);             // If a character pointer received
else if(b==3)
printf(“%f”,*(float*)a);             // If a float pointer received
}

Q3. State the equivalent pointer expression which refer the given array element
a[i][j][k][l]?
Ans. Above pointer expression can be written as *(*(*(*(a+i)+j)+k)+l).

Q4. Can you combine the following two statements into one?
char *p;
p = (char*)malloc(100);
Ans. Above two expressions can be written in combined form as
char*p = (char*)malloc(100);

Q5. How many bytes are occupied by near, far and huge pointers(DOS)?
Ans. Near occupies 2 bytes, far and huge can occupy 4 bytes each.

Q6. What will be output of following program?
#include
int main() {
int a = 10;
void *p = &a;
int *ptr = p;
printf(“%u”,*ptr);
return 0;
}

Ans. Output :10
Its because void can hold the address of any data type without type casting. Any Pointer can hold void pointer without type casting.

Q7. What will be output of following program?
#include
int main() {
int a = 320;
char *ptr;
ptr =(char *)&a;
printf(“%d”,*ptr);
return 0;
}

Ans. Output :64
Because int is two byte data type while char is one byte data byte. Char pointer can keep the address one byte at a time. Binary value of 320 is 00000001 01000000 (In 16 bit) ptr is pointing only first 8 bit which contain decimal value is 64.

Q8. What will be output of following program?
#include
#include
int main() {
char *ptr1 = NULL;
char *ptr2 = 0;
strcpy(ptr1,”c”);
strcpy(tr2,”questions”);
printf(“\n%s%S”,ptr1,ptr2);
return 0;
}

Ans. Output: (null)(null)
we cannot assign any string constant in null pointer by strcpy function.

Q9. What will be output of following program?
#include
#include
int main() {
int a = 5,b = 10,c;
int *p = &a,*q = &b;
c = p-q;
printf(“%d”,c);
return 0;
}

Ans. Output : 1
Because difference of two same type of pointer is always one.

Q10. What will be printed as the result of the operation below?
main()
{
char *p1;
char *p2;
p1=(char*)malloc(25);
p2=(char*)malloc(25);
strcpy(p1,”Cisco”);
strcpy(p2,”systems”);
strcat(p1,p2);
printf(“%s”,p1);
}

Ans. Output : Ciscosystems
Above operation can combine the two strings. Concatenation operation will be performed.

Download the Pointer based interview questions with answers in pdf POINTER- C Interview Questions and Answers.pdf

Feel free to give your suggestions and ask your questions to us in the comments. We will be highly pleased.

Related Links-
Functions in C Language
Interview questions and answers for C language
C++ Aptitude Questions and Answers with PDF

Tell us Your Queries, Suggestions and Feedback

Your email address will not be published.

One Response to Interview Questions on Pointer

  1. Anonymous says:

« »