Advertisement

help!

Started by August 19, 2002 08:50 AM
3 comments, last by bobbias 22 years, 2 months ago
can anyone see what''s wrong with this code? #include <iostream> #include <string> using namespace std; int test() { short MyAge; MyAge = 10; short *pAge = 0; pAge = &MyAge } int main() { test(); cout << "MyAge: " << MyAge << "\n"; cout << "*pAge: " << *pAge << "\n"; return 0; } the errors are: ----------Configuration: main - Win32 Debug---------- Compiling... main.cpp C:\Program Files\Microsoft Visual Studio\MyProjects\main\main.cpp(4) : error C2239: unexpected token ''{'' following declaration of ''main'' Error executing cl.exe. main.exe - 1 error(s), 0 warning(s) what''s wrong?
Weres who I got it to work:

#include <iostream>
#include <string>
using namespace std;

int test()
{
short MyAge;
MyAge = 10;
short *pAge;
pAge = &MyAge
return MyAge,*pAge;
}

int main()
{
short MyAge=test();
short pAge=test();
cout << "MyAge: " << MyAge << "\n";
cout << "*pAge: " << pAge << "\n";
return 0;
}

I can see also that you need to learn more about pointers because something like "short *pAge =0" would access memory address 0 and possible screw up your system.Also "cout << *pAge" would print some huge number so use pAge or &pAge.
Advertisement
quote: Original post by bobbias
can anyone see what''s wrong with this code?

Yes...
int test(){short MyAge;MyAge = 10;short *pAge = 0;pAge = &MyAge} 

This function is declared as returning an int. That means you must return an int, or if you don''t want a return value, you should declare it as returning "void".
int main(){test();cout << "MyAge: " << MyAge << "\n";cout << "*pAge: " << *pAge << "\n";return 0;} 


In main(), MyAge and pAge do not exist. They only exist within the function test(), where they were declared. What is it that you are trying to learn about with this sample code?
quote: Original post by Scet

int test(){short MyAge;MyAge = 10;short *pAge;pAge = &MyAgereturn MyAge,*pAge;}  

This does not do what you think it does. There is no way in C++ to return multiple values from a function, so what happens here is the *comma operator* is invoked and only the second value will be returned.
int main(){short MyAge=test();short pAge=test();cout << "MyAge: " << MyAge << "\n";cout << "*pAge: " << pAge << "\n";return 0;}  

When you call test() twice here, you seem to be expecting that the return in test() will behave like a "yield" in a language featuring generators. That is, you call it once and it returns MyAge, you call it a second time and it returns *pAge. It will actually return *pAge both times.
quote:
I can see also that you need to learn more about pointers because something like "short *pAge =0" would access memory address 0 and possible screw up your system.

No it wouldn't. It would assign the null pointer constant to pAge, indicating that pAge points at an invalid region of memory. It's a perfectly legal technique.
quote:
Also "cout << *pAge" would print some huge number so use pAge or &pAge.

"cout << *pAge" is fine - it dereferences pAge to get the value it points at, and the outputs that value.

[edited by - SabreMan on August 19, 2002 10:45:38 AM]
thanks all.

This topic is closed to new replies.

Advertisement