Friday, 2 September 2016

Pointers Questions Set

Pointers
Data Structures 15CS33


1.      Given the following declarations:
            int x ;
            double d;
            int *p;
            double *q;
            Which of the following expressions are not allowed?
i)                    p = &x;
ii)                  p = &d;
iii)                q = &x;
iv)                q = &d
v)                  p = x;
            (December 2007)
            (5 marks)

2.      Show what would be printed from the following block:
            #include<stdio.h>
            void fun (int (*p)[3])
            {
                        printf ("\n%d %d %d", (*p)[0], (*p)[1], *p[2]);
                        return;
            }
            void main()
            {
                        /* local definitions */
                        int x [2][3] =  { { 4,5, 2}, { 7,6, 9}  };
                        /* statements */
                        fun (x);
                        fun (x+1);
                        return 0;
            }
            (December 2007)
            (6 marks)

            Output:
            4          5          2686792
7              6          2130567168

3.      What is pointer variable? Can we have multiple pointer to variable?
(June 2009)
(December 2009)
(June 2010)
(6 marks)
   
4.      Show the output of the following block:
            main()
            {
                        int num[5] = {3,4,6,2,1};
                        int *p = num;
                        int *q = num+2;
                        int *r = &num[1];
                        printf("\n%d %d", num[2], *(num+2));
                        printf("\n%d %d", *p, *(p+1));
                        printf("\n%d %d", *q, *(q+1));
                        printf("\n%d %d", *r, *(r+1));
            }
            (June 2009)
            (December 2010)
            (6 marks)

            Output:
            6          6
            3          4
            6          2
            4          6

5.      What is pointer to pointer?Write a C program to find the smallest element in an array (using pointer and function)
(December 2009)
(7 marks)

6.      Write the output of the following
#include<stdio.h>
main()
{
                  int a = 5;
                  int b = 7;
                  int *p = &a;
                  int *q = &b;
                  printf("\n%d", ++a);
                  printf("\n%d", ++(*p));
                  printf("\n%d", --(*q));
                  printf("\n%d", --b);
}
(December 2009)
(4 marks)

Output:
6
7
6
5


7.      Write a C program to read 10 integers and store them in an array using pointers. Print their sum and average.
(June 2010)
(5 marks)

8.      What is pointer? What are the uses of pointers?  How do you declare and initialize the pointers? How do you access the value pointed by the pointers?
(June 2010)
(December 2010)
(June 2012)
(December 2012)
(June 2013)
(June 2014)
(December 2014)
(5 marks)

9.      Write a C function to swap two numbers using a pointers.

      (June 2012)(5 marks)