C Technical Interview Questions on Pointers

These C Technical Interview Questions On Pointers post providing basic, intermediate and Hard level examples in C Pointers. The Practice of these examples makes you good in C programming.


What is the output of this C code?
#include<stdio.h>
void main()
{
int arr[5] = {10,20,30,40,50},i=1;
int* ptr = arr;
printf(“%d,%d”, *(ptr + ++i), *(i– +ptr));
}

  1. 10, 30
  2. 20, 20
  3. 20, 30
  4. 10, 20

Answer :

Ans : 2 ;

What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 97, *p = &i;
f(&i);
printf(“%d “, *p);
}
void f(int *p)
{
int j = 2;
p = &j;
printf(“%d “, *p);
}

  1. 2, Garbage value
  2. Garbage value, Garbage value
  3. 50, 97
  4. 2, 97

Answer :

Ans : 4

What is the output of this C code?
#include<stdio.h>
int main()
{
int i = 100;
int *ip1, *ip2;

ip1 = &i;
ip2 = ++ip1;
–ip1;
printf(“diff : %u\n”, ip2-ip1);
return 0;
}

  1. 1
  2. 2
  3. 4
  4. Garbage value

Answer :

Ans : 1 ; The address increment by size bytes only but it prints the difference is always 1

What is the output of this C code?
#include <stdio.h>
void main()
{
char *s= “hello”;
char *p = s;
printf(“%c\t%c”, p[0], s[1]);
}

  1. h , garbage character
  2. h, h
  3. h , e
  4. Garbage character, e

Answer :

Ans : 3 ;

What is the output of this C code?
#include <stdio.h>
void main()
{
char *s= “hello”;
char *p = s;
printf(“%c\t%c”, *(p + 3), s[1]);
}

  1. l , e
  2. l , l
  3. l ,o
  4. k, e

Answer :

Ans : 1 ;

What is the output of this C code?
#include<stdio.h>
void main()
{
int arr[5] = {10,20,30,40,50};
int *ptr[5], i;
for(i=0 ; i<5 ; i++)
{
*(ptr+i) = arr+i;
}
printf(“%d\n”, *(*(ptr + 2)+1)+3);
printf(“%d\n”, *(*ptr+1)+3);
}

  1. Garbage value, Garbage value
  2. 43 , Garbage value
  3. 43, 23
  4. Compile time error

Answer :

Ans : 3 ;

What is the output of the code given below?
#include <stdio.h>
int main()
{
int ary[4] = {1, 2, 3, 4};
int *p = ary + 3;
printf(“%d\n”, p[-2]);
}

  1. 2
  2. Garage value
  3. 1
  4. Runtime error : No such location

Answer :

Ans : 1 ;

What is the output of this C code?
#include<stdio.h>
void main()
{
char sport[]= “sports”;
int x=1 , y;
y=x++ + ++x;
printf(“%c”,sport[++y]);
}

  1. null character
  2. s
  3. t
  4. garbage character

Answer :

Ans : 2 ;

 

Pointers – Technical – Part 3

Share this