locked
Exception throwing exception RRS feed

  • Question

  • int i = 10;
        int *ptr1 = & i;
        int **ptr2 ;
        *ptr2 = ptr1;
        int ***ptr3 ;
        **ptr3 = *ptr2;
        cout<<***ptr3;

    why this code throw exception after run
    Asia
    Wednesday, May 25, 2011 12:43 AM

Answers

  • On 5/24/2011 8:43 PM, Asia Choudhry wrote:

    int i = 10;
         int *ptr1 =&  i;
         int **ptr2 ;
         *ptr2 = ptr1;

    You are dereferencing an uninitialized pointer. ptr2 has never been assigned a value. You might have meant

    ptr2 = &ptr1;

         int ***ptr3 ;
         **ptr3 = *ptr2;

    Here, too.


    Igor Tandetnik

    • Marked as answer by Inayat Ullah Wednesday, May 25, 2011 1:29 AM
    Wednesday, May 25, 2011 12:50 AM

All replies

  • On 5/24/2011 8:43 PM, Asia Choudhry wrote:

    int i = 10;
         int *ptr1 =&  i;
         int **ptr2 ;
         *ptr2 = ptr1;

    You are dereferencing an uninitialized pointer. ptr2 has never been assigned a value. You might have meant

    ptr2 = &ptr1;

         int ***ptr3 ;
         **ptr3 = *ptr2;

    Here, too.


    Igor Tandetnik

    • Marked as answer by Inayat Ullah Wednesday, May 25, 2011 1:29 AM
    Wednesday, May 25, 2011 12:50 AM
  • i was thinking that *ptr2 is pointing to ptr and **ptr3 = *ptr2 how to link these three pointers i am still confused please guide me
    Asia
    Wednesday, May 25, 2011 12:56 AM
  • int i = 10;
        int *ptr1 = & i;
        int **ptr2 = 0  ;
        ptr2 = &ptr1;
        **ptr2 = 30;
        int ***ptr3 = &ptr2;
        ***ptr3 =**ptr2 ;
        cout<<***ptr3;

    i got it thanks


    Asia
    Wednesday, May 25, 2011 1:29 AM