Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. Consider the following three C functions
    [P1]   int*g(void)
      {
       intx = 10;
       return (&x);
      }
    [P2]   int*g(void)
      {
       int* px;
       *px = 10;
       return px;
      }
    [P3]   int*g(void)
      {
       int*px
       px = (int*) malloc (size of (int));
       *px = 10;
       return px;
      }
    Which of the above 3 functions are likely to cause problems with pointers ?
    1. Only P3
    2. P1 and P3
    3. P1 and P2
    4. P1, P2 and P3
Correct Option: C

P1 : Here the function is returning address of the variable x (& x) but the return type is pointer to integer not address. So incorrect.
P2 : *px = 0 directly assigned a value but still px doesn't point to any memory location, so memory initialization or allocation should be done before. So incorrect.
P3 : Correction made in P2, memory pre allocated, So correct.
Hence (c) is correct option.



Your comments will be displayed only after manual approval.