Re: [Dev-C++] Stress with structs and pointers.
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ricardo A. <ri...@te...> - 2003-06-12 08:11:22
|
> ). This incorporates everything you need to know:
>
> #include <iostream>
> #include <cstdlib>
> using namespace std;
>
> typedef struct test{
> public:
> int* a;
> test();
> };
>
> test::test(){
> a = new(int);
> *a = 5;
> cout << "In constructor: " << *a << endl;
> }
>
> int main(){
> test *x = new test();
> cout << "*(x->a): " << *(x->a) << endl;
> cout << "*((*x).a): " << *((*x).a) << endl;
> system("pause");
> }
>
Not bad, but you should free the memory previously allocated:
int main(){
test *x = new test();
cout << "*(x->a): " << *(x->a) << endl;
cout << "*((*x).a): " << *((*x).a) << endl;
system("pause");
delete x->a
delete x;
}
Regards
--
---------------------------------------------------
Ricardo Amores Hernández
ICQ: 19463735
MSN: zheo_ (at) hotmail (dot) es
---------------------------------------------------
|