Thread: Explain this code to me in English

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    1

    Post Explain this code to me in English

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int a[]={1,2,3,4};
      int *arr[5]={a,a+1,a+2,a+3,a+4};
      int *p=a+4;
      cout<<p[*arr[1]-5];
      return 0;
    }
    I was solving the programming question when I encountered this.
    My answer to this code was a printed garbage value. But, turns out the answer is 2.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    876
    English and a picture:

    After initializing a, arr and p you have:

    Explain this code to me in English-g-png

    *arr[1] will give you 2, since p points after the last element of a, p[2-5] will give you p[-3]. This gives you 2 also.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-18-2013, 03:37 PM
  2. Replies: 15
    Last Post: 04-17-2013, 09:00 AM
  3. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  4. how do u explain this in english words
    By Agnesa in forum C++ Programming
    Replies: 4
    Last Post: 02-10-2003, 06:27 PM
  5. how convert english to morse code
    By uler in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2001, 07:56 PM

Tags for this Thread