GeeksforGeeks » C/C++ Programming Questions

what is the output ?? and why

(2 posts)

Tags:

  1. daemon
    Member
    Posted 3 months ago #

    int a[1],b[1];
    a[0]=1;
    b[0]=2;
    b[1]=4;
    int *num1;
    num1 = &b[0];
    a[1]= static_cast<int>(*(num1));
    cout<<a[0]<<endl<<a[1]<<endl<<b[0]<<endl<<b[1];

  2. kartik
    Moderator
    Posted 3 months ago #

    Output seems to be unpredictable as the size of arrays is 1 and the code accesses a[1] and b[1]

    #include<iostream>
    
    using namespace std;
    
    int main()
    {
        int a[1],b[1];
        a[0]=1;
        b[0]=2;
        b[1]=4;
        int *num1;
        num1 = &b[0];
        a[1]= static_cast<int>(*(num1));
           cout<<a[0]<<endl<<a[1]<<endl<<b[0]<<endl<<b[1];
      return 0;
    }
    

Reply

You must log in to post.

RSS feed for this topic