• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

C Pointer Basics

Question 11

C
#include<stdio.h> 
void f(int *p, int *q) 
{ 
  p = q; 
  *p = 2; 
} 
int i = 0, j = 1; 
int main() 
{ 
  f(&i, &j); 
  printf(\"%d %d", i, j); 
  getchar(); 
  return 0; 
}
  • 2 2

  • 2 1

  • 0 1

  • 0 2

Question 12

Consider this C code to swap two integers and these five statements after it: C
void swap(int *px, int *py) 
{ 
   *px = *px - *py; 
   *py = *px + *py; 
   *px = *py - *px; 
}
S1: will generate a compilation error S2: may generate a segmentation fault at runtime depending on the arguments passed S3: correctly implements the swap procedure for all input pointers referring to integers stored in memory locations accessible to the process S4: implements the swap procedure correctly for some but not all valid input pointers S5: may add or subtract integers and pointers.
  • S1
  • S2 and S3
  • S2 and S4
  • S2 and S5

Question 13

C
int f(int x, int *py, int **ppz) 
{ 
  int y, z; 
  **ppz += 1; 
   z  = **ppz; 
  *py += 2; 
   y = *py; 
   x += 3; 
   return x + y + z; 
} 
  
void main() 
{ 
   int c, *b, **a; 
   c = 4; 
   b = &c; 
   a = &b; 
   printf(\"%d \", f(c, b, a)); 
   return 0;
}
  • 18
  • 19
  • 21
  • 22

Question 14

Predict the output of following program C
#include<stdio.h>
int main()
{
    int a = 12;
    void *ptr = (int *)&a;
    printf(\"%d\", *ptr);
    getchar();
    return 0;
}
  • 12
  • Compiler Error
  • Runt Time Error
  • 0

Question 15

C
#include<stdio.h>

void swap (char *x, char *y)
{
    char *t = x;
    x = y;
    y = t;
}

int main()
{
    char *x = \"geeksquiz\";
    char *y = \"geeksforgeeks\";
    char *t;
    swap(x, y);
    printf(\"(%s, %s)\", x, y);
    t = x;
    x = y;
    y = t;
    printf(\"\\n(%s, %s)\", x, y);
    return 0;
}
  • (geeksquiz, geeksforgeeks)
    (geeksforgeeks, geeksquiz)
    
  • (geeksforgeeks, geeksquiz)
    (geeksquiz, geeksforgeeks)
    
  • (geeksquiz, geeksforgeeks)
    (geeksquiz, geeksforgeeks)
    
  • (geeksforgeeks, geeksquiz)
    (geeksforgeeks, geeksquiz)
    

Question 16

C
#include <stdio.h>
int main()
{
    int arr[] = {1, 2, 3, 4, 5};
    int *p = arr;
    ++*p;
    p += 2;
    printf(\"%d\", *p);
    return 0;
}
  • 2
  • 3
  • 4
  • Compiler Error

Question 17

C
#include <stdio.h>
void f(char**);
int main()
{
    char *argv[] = { \"ab\", \"cd\", \"ef\", \"gh\", \"ij\", \"kl\" };
    f(argv);
    return 0;
}
void f(char **p)
{
    char *t;
    t = (p += sizeof(int))[-1];
    printf(\"%s\\n\", t);
}
  • ab
  • cd
  • ef
  • gh

Question 18

What does the following C-statement declare? [1 mark] C
int ( * f) (int * ) ;
  • A function that takes an integer pointer as argument and returns an integer.
  • A function that takes an integer as argument and returns an integer pointer.
  • A pointer to a function that takes an integer pointer as argument and returns an integer.
  • A function that takes an integer pointer as argument and returns a function pointer

Question 19

Consider the C program shown below.
C
#include <stdio.h>
#define print(x) printf(\"%d \", x)
int x;
void Q(int z)
{
    z += x;
    print(z);
}
void P(int *y)
{
    int x = *y + 2;
    Q(x);
    *y = x - 1;
    print(x);
}
main(void)
{
    x = 5;
    P(&x);
    print(x);
}
The output of this program is
  • 12 7 6

  • 22 12 11

  • 14 6 6

  • 7 6 6

Question 20

Suppose that in a C program snippet, followings statements are used. C
i) sizeof(int);
ii) sizeof(int*);
iii) sizeof(int**);
Assuming size of pointer is 4 bytes and size of int is also 4 bytes, pick the most correct answer from the given options.
  • Only i) would compile successfully and it would return size as 4.
  • i), ii) and iii) would compile successfully and size of each would be same i.e. 4
  • i), ii) and iii) would compile successfully but the size of each would be different and would be decided at run time.
  • ii) and iii) would result in compile error but i) would compile and result in size as 4.

There are 43 questions to complete.

Last Updated :
Take a part in the ongoing discussion