• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

need some help in c++

Pinkpig

Member
Hello all,

Im starting to play around with pointers in c++, i made a little program to see how pointers work. I cant seem to compile the code. it gives me error. can anyone take a look at the code that i included and see if i left anything out?
1st error - error C2440: '=' : cannot convert from 'int *' to 'int'
2nd error - error C2100: illegal indirection

#include <iostream.h>

int main()
{



int x;
x = 10;
cout << "x = " << x << '\n' <<endl;

int *p;
p= new int(10);
cout << "P = " << *p << endl;

int* p1,p2;
p1= new int;
*p1 = 42;
p2 = p1; // first error is here.

cout << "*p1 = " << *p1 << endl;
cout << "*p2 = " << *p2 << endl; //2nd type of error C2100: illegal indirection error here i dunno why =|

*p2 = 53; // 2nd type error here also

cout << "*p1 = " << *p1 << endl;
cout << "*p2 = " << *p2 << endl; // 2nd type error here also.

p1 = new int;
*p1 = 88;

cout << "*p1 = " << *p1 << endl;
cout << "*p2 = " << *p2 << endl; // 2nd error here also






return 0;

}


any help or comments would be great!

thanks
laura
 
On the line where you have

int *p1, p2;

change that to

int *p1, *p2;

After doing that, the code you posted compiled for me. The way you had it p1 was a pointer to an int and p2 was just a regular ol' int.
 
Originally posted by: cjohnson
On the line where you have

int *p1, p2;

change that to

int *p1, *p2;

After doing that, the code you posted compiled for me. The way you had it p1 was a pointer to an int and p2 was just a regular ol' int.

That doesn't seem right to me.
int* is what is being declared on that line. It's declaring both p1 and p2. It isn't declaring an int called *p1 and an int called *p2.
I don't see the error, but I'm not really looking. 🙂
 
int *p1, *p2 makes an int* (pointer to an int) named p1 and another int* named p2. The location of the asterisks is irrelevant. What you had is equivalent to

int * p1;
int p2;

What i'm telling you to do is equivalent to

int * p1;
int * p2;
 
Originally posted by: cjohnson
int *p1, *p2 makes an int* (pointer to an int) named p1 and another int* named p2. The location of the asterisks is irrelevant. What you had is equivalent to

int * p1;
int p2;

What i'm telling you to do is equivalent to

int * p1;
int * p2;

Ah I see, I thought there was an actual type. They keep us too focused on Java here at school 🙁, haven't seen C++ in forever. I guess I'm half to blame for that though.
 
Back
Top