What is the different between int *x and int * x?
selenium (11)
Dec 10, 2012 at 3:51am UTC
I thought int * x is for declaring a pointer, but what does the int *b, and *c do?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
// pointer to classes example
#include <iostream>
using namespace std;
class CRectangle {
int width, height;
public :
void set_values (int , int );
int area (void ) {return (width * height);}
};
void CRectangle::set_values (int a, int b) {
width = a;
height = b;
}
int main () {
CRectangle a, *b, *c;
CRectangle * d = new CRectangle[2];
b= new CRectangle;
c= &a;
a.set_values (1,2);
b->set_values (3,4);
d->set_values (5,6);
d[1].set_values (7,8);
cout << "a area: " << a.area() << endl;
cout << "*b area: " << b->area() << endl;
cout << "*c area: " << c->area() << endl;
cout << "d[0] area: " << d[0].area() << endl;
cout << "d[1] area: " << d[1].area() << endl;
delete [] d;
delete b;
return 0;
}
maeriden (230)
Dec 10, 2012 at 4:00am UTC
When you declare a variable the asterisk means that they are pointers
When you use a pointer the asterisk means that you want to use the content of the pointer
int *b, *c; are two pointers to int
Last edited on Dec 10, 2012 at 4:00am UTC
Athar (4379)
Dec 10, 2012 at 4:00am UTC
They declare pointers.
selenium (11)
Dec 10, 2012 at 4:57am UTC
so both int * X and int *X are the same?
cire (1845)
Dec 10, 2012 at 5:25am UTC
And so is int* X.