Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

I have a c++ programming question. I need to know the output of this code.?

int data[5] = {1, 2, 3, 4, 5};

int *p = data;

for(int i = 0; i < 5; i++, p++) {

*p = 5 - i;

p[3] = -100;

p--;

}

for(int i = 0; i < 5; i++, p--){

cout << ‘ ‘ << *p;

}

cout << endl;

cout << *p << endl;

Update:

i understand i need headers to COMPILE to code... i am not interested in getting this code to work... i just need to know the output of this code.. ASSUME it is syntacticaly correct and what will the out put be.

5 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    compile it and see!

    int data[5] = {..};

    define an array...

    int *p = data;

    let p be a pointer to the array (so now p points to the first element of the array, ie 1)

    for(int i = 0; i < 5; i++, p++) {

    *p = 5 - i;

    p[3] = -100;

    p--;

    }

    let us study it step by step:

    iter 1: i==0;

    *p is the 1... now it becomes 5-0; it is as if data[0]=5;

    p[3] is the same as data[3]= -100;

    p--

    let the p pointer goes a step back... now p is poiting to an area we should not address...! but wait, the update condition of the loop say p++, so magically now p points to the first element of data array again! basically, p stays the same through the cycle

    iter 2: i==1, p still pointing to first el of data ( &data[0] );

    we can "project" to the future, saying that the last time the loop is executed is the one that decide everything;

    the last time i==4, so:

    *p that is data[0] becomes 1,

    p[3] that is data[3] becomes -100

    p stay the same (since after the last iter, the i++ and p++ are executed, then the condition i<5 is tested again, found it false, the for loop exit)

    for(int i = 0; i < 5; i++, p--){

    cout << ‘ ‘ << *p;

    }

    this loop gets integers from memory that maybe does not belong to the process, and anyway you can't know what's inside;

    the first value is data[0] (1), the next ones, can't be known before.

    it makes a little bit of sense if you change p-- into p++ in the last loop

  • 1 decade ago

    If the program compiles then the output must be :

    1 2 3 -100 5

    1

    But most compilers would throw an error on this program...Because the iterations of both the loops executes 'p--' which means 'p' accesses memory space that is not allocated for use...'p' has allocated memory only frm a[0] to a[4]...There is no memory allocated before that...

    So it may throw a compile time error...

    Anyway all the best :-)

    Source(s): 6 years of programming practice !!!
  • 1 decade ago

    Copy and paste the following:

    #include <iostream>

    #include <string>

    #include <stdio.h>

    using namespace std;

    int data[5] = {1, 2, 3, 4, 5};

    int *p = data;

    for(int i = 0; i < 5; i++, p++) {

    *p = 5 - i;

    p[3] = -100;

    p--;

    }

    for(int i = 0; i < 5; i++, p--){

    cout << ‘ ‘ << *p;

    }

    cout << endl;

    cout << *p << endl;

    int main()

    {

    cout << " Enter any key to continue : ";

    enterdata();

    return 0;

    }

    // that should do it

  • 1 decade ago

    The reason people tell you to compile it is then you can answer your own question. Your code is NOT syntactically correct.

    It will output garbage. In your output loop you decrement p each iteration. Since p starts out pointing to data[0], you are printing unintialized areas of memory.

    Your first loop changes the array data from

    {1,2,3,4,5} to

    {1,2,3,-100,5}

    since p starts at data, you decrement it at the end of the loop and increment it at the next iteration. Last iteration is data[0]=5-4=1.

    Your output loop prints out

    data[0], whatever's one int in memory before data[0], whatever's two ints in memory before data[0], whatever's three ints in memory before data[0], and whatever's four ints in memory before data[0].

    And it wouldn't compile since you redefine the loop counter i.

  • How do you think about the answers? You can sign in to vote the answer.
  • Anonymous
    1 decade ago

    undefined behavior because the address of the p will be got changed five times so it will print a value from a garbage value

Still have questions? Get your answers by asking now.