what is line 5 doing?

Please, how did it come to line 5? What commands cause wrirting it out???

many thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  int* po(int b){
int d1;
d1=3*b;
int d2;
d2=d1+5;
int a[3];
a[0]=d1;
a[1]=d2;
a[2]=a[1]+3;
int* p=a;
cout<<"po: "<<p<<endl;
return p;
}


int main(){
int st=4;
cout<<" main: "<<po(st)<<"xx"<<endl<<endl<<endl;
int* h;
h=po(st);
cout<<endl;
cout<<"main: "<<*h<<endl;
return 0;
}
What do you mean by "line 5"?

Local array a[3] does not exist outside of the scope of the functio 'po' and therefore returning the address of the array is an error.
Hello!
I am getting this:

1
2
3
4
5
6
7
po: 0xbf57cff4
 main: 0xbf57cff4xx


po: 0xbf57cff4

main: 12


This 5th line is what i do not see its cause...!

It is obviously after line 18 is being executed, and BEFORE line 22 is executed...:roll???
Last edited on
You get first "po:" from calling the po() on line 18, the first " main:" on line 18, the second "po:" from second call of po() on line 20, and the second "main:" on line 22.
sure, but is line 20 supposed to cout it? That is confusing me!

many thnks!
Supposed? How could we know? It is your function po() that prints something on its line 11. You should know what your code is supposed to do.
Hello!
Line 11 causes the line 1 in executing.
Then: line 18 causes line 2 and 2 empty lines after it.(lines 3 and 4).

Then: U said line 20 causes line 5, but I do not understand this part.

Then: line 21 causes free line 6.
Then: line 22 causes line 7.


So: all clear, except line 21 causing line 5).

If I wanted var h not to be cout-ed, (but only *h), how should I write the code?
Many thanks!

Last edited on
You do call function po() twice. Function po() prints a line. Therefore, line is printed two times.

Solution: You either do not call the function multiple times or the function does not print a line.


However, like I said the first time, the rest of your code leads to undefined results and should be changed.
Hello!
I am working in codepad (http://codepad.org/), and I am NOT GETTING any undefined results.

I pasted the complete output under the code.

Now, look, line 20 h=po(st); definitiveljy COUTS the po*().

But, if I don't want it to be couted, but only *h, how to write it?


Many thanks!
I wrote:
Solution: ...



Note: Undefinded, by definition, can be anything. It can even look like the expected, valid result in some conditions, but that is just a fluke.
Topic archived. No new replies allowed.