Pointers(C Programming ) Questions and Answers

Question 1.


Will the following program give any warning on compilation in TurboC (under DOS)?


#include<stdio.h>
int main()
{
int *p1, i=25;
void *p2;
p1=&i;
p2=&i;
p1=p2;
p2=p1;
return 0;
}
  1.    Yes
  2.    No
Explanation:-
Answer: Option B. -> No

No answer description available for this question. 



Question 2.


Will the program compile in Turbo C?


#include<stdio.h>
int main()
{
int a=10, *j;
void *k;
j=k=&a;
j++;
k++;
printf("%u %u\n", j, k);
return 0;
}
  1.    Yes
  2.    No
Explanation:-
Answer: Option B. -> No

Error in statement k++. We cannot perform arithmetic on void pointers.

The following error will be displayed while compiling above program in TurboC.



Question 3.

Is the NULL pointer same as an uninitialised pointer?


  1.    Yes
  2.    No
Explanation:-
Answer: Option B. -> No

No answer description available for this question. 



Question 4.

Is this a correct way for NULL pointer assignment?
int i=0;
char *q=(char*)i;


  1.    Yes
  2.    No
Explanation:-
Answer: Option B. -> No

The correct way is char *q=0 (or) char *q=(char*)0



Question 5.

Is there any difference between the following two statements?
char *p=0;
char *t=NULL;


  1.    Yes
  2.    No
Explanation:-
Answer: Option B. -> No

NULL is #defined as 0 in the 'stdio.h' file. Thus, both p and t are NULL pointers.



Question 6.

Are the three declarations char **apple, char *apple[], and char apple[][] same?


  1.    True
  2.    False
Explanation:-
Answer: Option B. -> False

No answer description available for this question. 



Question 7.


The following program reports an error on compilation.


#include<stdio.h>
int main()
{
float i=10, *j;
void *k;
k=&i;
j=k;
printf("%f\n", *j);
return 0;
}
  1.    True
  2.    False
Explanation:-
Answer: Option B. -> False

This program will NOT report any error. (Tested in Turbo C under DOS and GCC under Linux) 
The output: 10.000000



Question 8.


Which of the statements is correct about the program?


#include<stdio.h>
int main()
{
int arr[3][3] = {1, 2, 3, 4};
printf("%d\n", *(*(*(arr))));
return 0;
}
  1.    Output: Garbage value
  2.    Output: 1
  3.    Output: 3
  4.    Error: Invalid indirection
Explanation:-
Answer: Option D. -> Error: Invalid indirection



Question 9.


In the following program add a statement in the function fact() such that the
factorial gets stored in j.


#include<stdio.h>
#include
void fact(int*);
int main()
{
int i=5;
fact(&i);
printf("%d\n", i);
return 0;
}
void fact(int *j)
{
static int s=1;
if(*j!=0)
{
s = s**j;
*j = *j-1;
fact(j);
/* Add a statement here */
}
}
  1.    j=s;
  2.    *j=s;
  3.    *j=&s;
  4.    &j=s;
Explanation:-
Answer: Option B. -> *j=s;

No answer description available for this question. 



Question 10.

Are the expression *ptr++ and ++*ptr are same?


  1.    True
  2.    False
Explanation:-
Answer: Option B. -> False

*ptr++ increments the pointer and not the value, whereas the ++*ptr increments

the value being pointed by ptr