Re: [Dev-C++] Stress with structs and pointers.
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Glen V. <gve...@pa...> - 2003-06-12 07:17:06
|
1) Structs also support constructors (see below for example program)
2) Don't know what you mean. But if you mean that it can take any type
of members: yes. If you want to get to that member, just do it the same
way: struct.membername. It's the same way for pointers or other variables.
3) *(struct.membername). It's very easy to test this yourself.
4) plain logic: you got a pointer to struct, so use struct->member. The
variable is a pointer, so dereference the endresult: *(struct->member).
*((*struct).member) would be an alternative way (dereference your
pointer to struct, get its member, dereference that) :)
I wrote a little test to be sure (and in the process I learned stuff too
:) ). 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");
}
David McKen wrote:
> Ok, I need some clarification on a couple topics invloving pointers
> and pointers.
>
> 1) when a struct is created. All that can be assumed is that it
> contains trash right? What is a portable way to initalize everything
> to 0? mset?
>
> 2) when a structure contains a pointer. Access to the address (not the
> value) comes in the form {struct_variable_name}.{member_name}?
>
> 3) how do you de-refrence the pointer in #2 to get the value of the
> variable? {struct_variable_name}.*{member_name}?
>
> 4) if you have a pointer to the structure you access the members via
> '->'. If a member of the structure is a pointer the address is gotten
> via {struct_varible_name}->{member_name}? If so how does one get the
> value of the varible? {struct_variable_name}->*{member_name}?
>
> Thanks in Advance
> David McKen
>
> P.S. Was begining to run out of hair!
> ------------------------------------------------------------------------
> Do you Yahoo!?
> Free online calendar
> <http://us.rd.yahoo.com/mail_us/tag/*http://calendar.yahoo.com> with
> sync to Outlook(TM).
|