Home »
Aptitude Questions and Answers »
C Aptitude Questions and Answers
C Pointers - Aptitude Questions & Answers
C programming Pointers Aptitude Questions and Answers: In this section you will find C Aptitude Questions and Answers on Pointers topics, declaring pointers, using pointers, pointer with other c topics like pointer of array etc.
1) What will be the output of following program ?
#include <stdio.h>
int main()
{
char *str="IncludeHelp";
printf("%c\n",*&*str);
return 0;
}
- Error
- IncludeHelp
- I
- *I
Correct Answer - 3
I
& is a reference operator, * is de-reference operator, We can use these operators any number of times.
str points the first character of IncludeHelp, *str points "I", * & again reference and de-reference the value
of str.
2) What will be the output of following program ?
#include <stdio.h>
int main()
{
int iVal;
char cVal;
void *ptr; // void pointer
iVal=50; cVal=65;
ptr=&iVal;
printf("value =%d,size= %d\n",*(int*)ptr,sizeof(ptr));
ptr=&cVal;
printf("value =%d,size= %d\n",*(char*)ptr,sizeof(ptr));
return 0;
}
- Error
- value =50,size= 4
value =65,size= 4
- value =50,size= 4
value =65,size= 1
- Garbage Values
Correct Answer - 2
value =50,size= 4
value =65,size= 4
void pointer can be type casted to any type of data type, and pointer takes 4 bytes (On 32 bit compiler).
to print value using void pointer, you will have to write like this *(data_type*)void_ptr;.
3) What will be the output of following program ?
#include <stdio.h>
int main()
{
char *str []={"AAAAA","BBBBB","CCCCC","DDDDD"};
char **sptr []={str+3,str+2,str+1,str};
char ***pp;
pp=sptr;
++pp;
printf("%s",**++pp+2);
return 0;
}
- BBBBB
- CCCCC
- BBB
- Error
Correct Answer - 3
BBB
*str is a array pointer of string, **sptr is array pointer(double pointer) that is pointing to
str strings in reverse order. ***pp also a pointer that is pointing sptr base address.
++pp will point to 1st index of sptr that contain str+2 ("CCCCC").
in printf("%s",**++pp+2); ++pp will point to str+1, and **++pp, value stored @ str+1 ("BBBBB).
and (**++pp)+2 will point the 2nd index of "BBBBB", hence BBB will print.
4) What will be the output of following program ?
#include <stdio.h>
char* strFun(void)
{
char *str="IncludeHelp";
return str;
}
int main()
{
char *x;
x=strFun();
printf("str value = %s",x);
return 0;
}
- str value = Garbage value
- str value = IncludeHelp
- Error
- No output
Correct Answer - 2
str value= IncludeHelp
5) If the address of pointer ptr is 2000, then what will the output of following program ?
[On 32 bit compiler.]
#include <stdio.h>
int main()
{
void *ptr;
++ptr;
printf("%u",ptr);
return 0;
}
- 2004
- 2001
- 2000
- ERROR
Correct Answer - 4
ERROR: Size of the type is unknown or zero.
ptr is a void pointer, and the scale factor of void pointer is unknown or zero.
6) What will be the output of following program ?
#include <stdio.h>
int main()
{
char ch=10;
void *ptr=&ch;
printf("%d,%d",*(char*)ptr,++(*(char*)ptr));
return 0;
}
- 11,11
- 10,11
- ERROR
- 10,10
Correct Answer - 1
11,11
*(char*)ptr will return the value of ch, since we know printf evaluates right to left..
so, ++(*(char*)ptr) will increase the value to 11.
7) What will be the output of following program ?
#include <stdio.h>
int main()
{
int a=10,b=2;
int *pa=&a,*pb=&b;
printf("value = %d", *pa/*pb);
return 0;
}
- 5
- 5.0
- ERROR
- None of these
Correct Answer - 3
ERROR: unexpected end of file found in comment.
The compiler is treated the operator / and * as /*, that happens to be the
starting of comment.
To fix the error, use either *pa/ *pb (space between operators) or *pa/(*pb).
8) What will be the output of following program ?
#include <stdio.h>
void fun(int *ptr)
{
*ptr=100;
}
int main()
{
int num=50;
int *pp=#
fun(& *pp);
printf("%d,%d",num,*pp);
return 0;
}
- 100,100
- 50,50
- 50,100
- ERROR in function calling
Correct Answer - 1
100,100