Author |
Message |
omni
|
Posted: Sat Feb 12, 2005 10:18 pm Post subject: Pointers to structures |
|
|
This question seems a little newb compared to the other ones on this forum, but anyway ...
struct exmstruct{
int x;
int y;
int z;
}
...
exmstruct jeff;
exmstruct *expointer;
expointer = &jeff;
Ok so where will "expointer" be pointing to? Will it be the jeff.x? And if I do "expointer += 1" will the pointer be pointing to jeff.y after?
EDIT: Not sure if i'm thinking of structure pointers the right way. I am thinking of structure pointers like array and string pointers. Is this right? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Sun Feb 13, 2005 12:21 am Post subject: (No subject) |
|
|
They're entirely the same. A pointer is a pointer is a pointer. It's simply a variable which holds the address in memory of something else.
c++: | struct example_struct
{
int x, y, z;
};
int main()
{
// foo is just an example_struct object
example_struct foo;
// bar is a pointer to an example_struct object
example_struct * bar;
// &foo gets the memory address for foo
// the rest assigns that memory address to the bar pointer
bar = &foo;
// the above could have been better written:
// example_struct * bar = &foo;
return 0;
} |
Now, theoretically bar points to the first member of example_struct, or x. However, there is no guarantee in C++ that members will be laid out in a given fashion, and memory may be padded in such a way that doing pointer arithmetic of this sort is evil.
The simple answer is: don't do it. Use foo.x, foo.y, foo.z. |
|
|
|
|
|
Andy
|
Posted: Sun Feb 13, 2005 2:20 pm Post subject: (No subject) |
|
|
c++: |
struct exmstruct
{
int x, y, z;
};
int main()
{
exmstruct jeff;
exmstruct *expointer;
expointer = &jeff;
expointer ->x=0;
expointer ->y=0;
expointer ->z=0;
}
|
u use -> to access the sub variables |
|
|
|
|
|
wtd
|
Posted: Sun Feb 13, 2005 5:32 pm Post subject: (No subject) |
|
|
Andy wrote: c++: |
struct exmstruct
{
int x, y, z;
};
int main()
{
exmstruct jeff;
exmstruct *expointer;
expointer = &jeff;
expointer ->x=0;
expointer ->y=0;
expointer ->z=0;
}
|
u use -> to access the sub variables
"members"
Structs in C++ are simply classes where everything's public by default. |
|
|
|
|
|
Andy
|
Posted: Sun Feb 13, 2005 7:52 pm Post subject: (No subject) |
|
|
o oops.. it doesnt work on structs? hmmmm heh use classes |
|
|
|
|
|
md
|
Posted: Sun Feb 13, 2005 8:30 pm Post subject: (No subject) |
|
|
no no, -> works on structs in C++, but structs are really just classes where everything is public |
|
|
|
|
|
Mazer
|
Posted: Mon Feb 14, 2005 10:13 am Post subject: (No subject) |
|
|
By default. You can still make members protected/private, right? |
|
|
|
|
|
wtd
|
Posted: Mon Feb 14, 2005 12:36 pm Post subject: (No subject) |
|
|
Coutsos wrote: By default. You can still make members protected/private, right?
Absolutely.
You can subclass structs, too. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
omni
|
Posted: Mon Feb 14, 2005 7:58 pm Post subject: (No subject) |
|
|
If I want to create an array of strings, would I just make a 2d array? |
|
|
|
|
|
Andy
|
Posted: Mon Feb 14, 2005 8:01 pm Post subject: (No subject) |
|
|
didnt u learn the string class??? u guys didnt do classes??? |
|
|
|
|
|
omni
|
Posted: Mon Feb 14, 2005 8:09 pm Post subject: (No subject) |
|
|
I'm self teaching myself this. So there is no "guys" And no I didn't learn string classes or classes in general : / |
|
|
|
|
|
Andy
|
Posted: Mon Feb 14, 2005 8:14 pm Post subject: (No subject) |
|
|
windsor eh? do u go to massey? anyways, you should read up on classes, they are very very useful |
|
|
|
|
|
Mazer
|
Posted: Mon Feb 14, 2005 8:18 pm Post subject: (No subject) |
|
|
omni: yes, a 2D char array will give you an array of strings. That said, character arrays can really be a pain. You should look up the std string class when you get a chance.
Also, find "Thinking in C++" if you can. It's floating around the internet for free and supposedly a good read. (I should read it myself when I have some time). |
|
|
|
|
|
omni
|
Posted: Mon Feb 14, 2005 8:20 pm Post subject: (No subject) |
|
|
@ Andy : yea i go to massey gr.11
@coustos: K i'll look it up
BTw is there any command to clear the screen? using std::cout all the time just makes the screen scroll down and that doesnt really look nice. |
|
|
|
|
|
Mazer
|
Posted: Mon Feb 14, 2005 8:33 pm Post subject: (No subject) |
|
|
As far as I know, yes, but they aren't very good. Ultimately you must ask, do you really need to clear the console screen? |
|
|
|
|
|
|