Friday, October 26, 2012

Solution to E Balaguruswamy Chapter - Pointers

                                                               Solution to E Balaguruswamy
                                                                     Chapter  - Pointers

Ans 11.4 Pointer is initialized by keyword NULL or  0  in the following fashion
int  * p = NULL  , * m = 0 ;
 Ans 11.5  Following is the effect of the following statements
i.                     int a, *b = &a;  //Error Pointer being initialized with the address of an uninitialized variable
ii.                   int p ,   *p ;      //Error Multiple Declaration of p
iii.                  char *s;          
iv.                 a = (float * ) &x);
v.                   double  ( *f )( ) ;       //It declares the pointer to a function

Ans 11. 6   int M ,  n ;
    int *P1  , *P2 ;
The following are the errors in the expression given below:
i)                    P1 = &m ; //Error Pointer cannot be initialized with an uninitialized variable
ii)                   P2 = n ; // Error Pointer can only be initialized with the address of the variable
iii)                 P2 = &*&m; // Correct as the first dereferencing operator Is over powered by the second & operator
iv)                  M = P2 – P1 //Correct Pointer Subtraction Is allowed
v)                  P1 = &P2 //Error A simple pointer cannot be initialized with memory address of an another pointer, only a pointer to a pointer can be initialized in this way
  Ans11.7 Difference between the notations  *m[5] and (*m)[3]. Since * has lower precedence than  [] , *m[5] declares m  as an array of 5 pointers while (*m)[3] declares m as a pointer to an array of five elements.
Ans 11.9   int x = 10 ,  y  = 10 ;
                    int  *p1 = &x , *p2 = &y;
                The following Expression generate the following output:
i)                    (*p1)++   Output – 10
ii)                   - - (*p2)   Output  -  9
iii)                 *p1 + (*p2)--   Output  - 20
iv)                   ++(*p2) - *p1  Output  -  1
Ans 11.12  Following Output is  generated by the following code :
                int m = 100 ;
                int *p1 = &m;
                int **p2 = &p1;
                printf(“%d” , **p2);
                Output :
                100
Ans 11. 13   Following is the error in the given code
                                int **p1 , * p2 ;
                                p2 = &p1  // It should be  p1 =&p2
Explanation: p1 is a pointer to a pointer so it should always point a pointer
Ans11.14  Difference between the two expression name + 10; and *(name + 10 ) is that the first expression refers to all the characters after the 10th position in the array name or rather simply we  can say that it will the array name from the 10th character whereas the second expression refers specifically to the 10th character of the array of name .
Ans 11.15  Following is the output of the code given below
                                int m[2] ;
                                *(m+1)
                                *m = *(m + 1);
                                printf(“ %d “ , m[0] );
Output:
                                100
Ans 11.16    int m[2];
                       int *p = m ;
                       m[0] = 100;
                       m[1] = 200;
                       printf(“%d %d “ , ++*p , *p );
                      Output:
                      101  100
Ans 11.17    Output of the following code :
                 int f(char *p );
                main()
                {              char str[] = “ANSI”;
                                printf(“%d” , f(str) );
                }
                int f(char *p )
                {              char *q = p ;
                                while (*++p ) ;
                                return (p – q );                  }
Output :
4
Ans 11.18  Following declarations are not the same:
                1 . void search ( int *m[] , int x) { }
                In this the parameter in the definition is a integer pointer array.
                2. void search ( int **m  ,int x ) {}
                In this the parameter in the definition is a integer pointer to a pointer.
Ans 11.19   Following declaration is are same
                char *s;
                char s[5] ;
                As name of the array is a pointer to the first element of the array it’s as same as declaring a pointers which when incremented can be used to store a list of values same as an array.
Ans 11.20  The following is the correct way to declare a pointer to a function.
                int (*p)(void);
                

4 comments:

  1. Thanks it was of great help 2 me

    ReplyDelete
  2. Thanks! for helping!

    ReplyDelete
  3. Why is int (*p)(void);the correct way to declare a pointer to a function?

    ReplyDelete