-
help with pointers
part of my software where i test certain crap out since im learning is about pointers.
Well I think im doin it right but it gives me errors.
Errors:
Code:
1>d:\users\ant1jr\documents\visual studio 2005\projects\test 1\test 1\test 1.cpp(106) : error C2101: '&' on constant
1>d:\users\ant1jr\documents\visual studio 2005\projects\test 1\test 1\test 1.cpp(106) : error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>d:\users\ant1jr\documents\visual studio 2005\projects\test 1\test 1\test 1.cpp(109) : error C2101: '&' on constant
1>d:\users\ant1jr\documents\visual studio 2005\projects\test 1\test 1\test 1.cpp(109) : error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>d:\users\ant1jr\documents\visual studio 2005\projects\test 1\test 1\test 1.cpp(112) : error C2101: '&' on constant
1>d:\users\ant1jr\documents\visual studio 2005\projects\test 1\test 1\test 1.cpp(112) : error C2440: '=' : cannot convert from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Code:
Code:
void Pointers()
{
int *p;
int 1;
int 2;
int 3;
int I;
cout<<"What do you want 1 to be?\n";
cin>> 1;
cin.ignore();
cout<<"What do you want 2 to be?\n";
cin>> 2;
cin.ignore();
cout<<"What do you want 3 to be?\n";
cin>> 3;
cin.ignore();
cout<<"What do you want the pointer to point to?\n";
cin>> I;
cin.ignore();
switch ( I ) {
case 1:
p = &1;
break;
case 2:
p = &2;
break;
case 3:
p = &3;
break;
}
}
-
Numbers are not valid variable names. How would your compiler tell if you wanted the variable or the actual value 2?
Edit: Also, assuming your code worked, if I entered "4" at the last question, then p could point to just about anything. Either reprompt the user for valid input, or use a default: and set it to something valid.
-
Always initalize a pointer on delcaration!!!!
I dont get what the code is trying to do, the indentation is poor. Could you post the whole program if possble?
-
doesn't look like it was meant to accomplish anything useful, he said he was just learning about pointers.
add a letter before 1, 2 and 3 or change the names