C Advanced Pointer

    Last Updated :
    Discuss
    Comments

    Question 1

    ‘ptrdata’ is a pointer to a data type. The expression *ptrdata++ is evaluated as (in C++) :
     

    • Depends on compiler
       

    • (*ptrdata)++
       

    • *(ptrdata)++
       

    • *(ptrdata++)
       

    Question 2

    What is printed by the following C program?

    C
    $include <stdio.h>
    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));
       getchar();
    }
    
    • 18

    • 19

    • 21

    • 22

    Question 3

    What will be output of the following program? Assume that you are running this program in little-endian processor.

    C
    #include<stdio.h>
    
    int main() {
        short a = 320;
        char * ptr;
        ptr = (char * ) & a;
        printf("%d", * ptr);
        return 0;
    }
    
    • 1

    • 320

    • 64

    • Compilation error

    Question 4

    Consider the following C function

    C
    #include <stdio.h>
    int main(void)
       {
        char c[ ] = "ICRBCSIT17";
        char *p=c;
        printf("%s", c+2[p]  6[p]  1);
        return 0;
       }
    

    The output of the program is

    • SI

    • IT

    • TI

    • 17

    Question 5

    Find out the correct statement for the following program. C
    #include "stdio.h"
    
    typedef int (*funPtr)(int);
    
    int inc(int a)
    {
     printf("Inside inc() %d\\n",a);
     return (a+1);
    }
    
    int main()
    {
    
     funPtr incPtr1 = NULL, incPtr2 = NULL;
    
     incPtr1 = &inc; /* (1) */
     incPtr2 = inc; /* (2) */
    
     (*incPtr1)(5); /* (3) */
     incPtr2(5); /* (4)*/
    
     return 0;
    }
    
    • Line with comment (1) will give compile error.
    • Line with comment (2) will give compile error.
    • Lines with (1) & (3) will give compile error.
    • Lines with (2) & (4) will give compile error.
    • No compile error and program will run without any issue.

    Question 6

    In the context of the below program snippet, pick the best answer.

    C
    #include <stdio.h>
    int arr[10][10][10];
    int main()
    {
     arr[5][5][5] = 123;
     return 0;
    }
    

    Which of the given printf statement(s) would be able to print arr[5][5][5]

    C
    (i) printf("%d",arr[5][5][5]);
    (ii) printf("%d",*(*(*(arr+5)+5)+5));
    (iii) printf("%d",(*(*(arr+5)+5))[5]);
    (iv) printf("%d",*((*(arr+5))[5]+5));
    
    • only (i) would compile and print 123.

    • both (i) and (ii) would compile and both would print 123.

    • only (i), (ii) and (iii) would compile but only (i) and (ii) would print 123.

    • only (i), (ii) and (iii) would compile and all three would print 123.

    • all (i), (ii), (iii) and (iv) would compile but only (i) and (ii) would print 123.

    • all (i), (ii), (iii) and (iv) would compile and all would print 123.

    Question 7

    C
    #include <stdio.h> 
    int main()
    {
     void *pVoid;
     pVoid = (void*)0;
     printf("%lu",sizeof(pVoid));
     return 0;
    }
    

    Pick the best statement for the above C program snippet.

    • Assigning (void *)0 to pVoid isn’t correct because memory hasn’t been allocated. That’s why no compile error but it'll result in run time error.

    • Assigning (void *)0 to pVoid isn’t correct because a hard coded value (here zero i.e. 0) can’t assigned to any pointer. That’s why it'll result in compile error.

    • No compile issue and no run time issue. And the size of the void pointer i.e. pVoid would equal to size of int.

    • sizeof() operator isn’t defined for a pointer of void type.

    Question 8

    C
    #include <stdlib.h>
    int main()
    {
     int *pInt;
     int **ppInt1;
     int **ppInt2;
    
     pInt = (int*)malloc(sizeof(int));
     ppInt1 = (int**)malloc(10*sizeof(int*));
     ppInt2 = (int**)malloc(10*sizeof(int*));
    
     free(pInt);
     free(ppInt1);
     free(*ppInt2);
     return 0;
    }
    

    Choose the correct statement w.r.t. above C program.

    • malloc() for ppInt1 and ppInt2 isn’t correct. It’ll give compile time error.

    • free(*ppInt2) is not correct. It’ll give compile time error.

    • free(*ppInt2) is not correct. It’ll give run time error.

    • No issue with any of the malloc() and free() i.e. no compile/run time error.

    Question 9

    Consider the following C program.

    C
    # include 
    int main( )
    {
      static int a[] = {10, 20, 30, 40, 50};
      static int *p[] = {a, a+3, a+4, a+1, a+2};
      int **ptr = p;
      ptr++;
      printf("%d%d", ptr - p, **ptr};
    }
    

    The output of the program is _________

    • 140

    • 120

    • 100

    • 40

    Question 10

    What is the output of the following C code? Assume that the address of x is 2000 (in decimal) and an integer requires four bytes of memory.

    C
    #include <stdio.h>
    int main()
    { 
       unsigned int x[4][3] = {{1, 2, 3}, {4, 5, 6}, 
                               {7, 8, 9}, {10, 11, 12}};
       printf("%u, %u, %u", x+3, *(x+3), *(x+2)+3);
    }
    
    • 2036, 2036, 2036

    • 2012, 4, 2204

    • 2036, 10, 10

    • 2012, 4, 6

    Tags:

    There are 19 questions to complete.

    Take a part in the ongoing discussion