Computer Science Canada Pointers to structures |
Author: | omni [ 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? |
Author: | wtd [ Sun Feb 13, 2005 12:21 am ] | ||
Post 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.
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. |
Author: | Andy [ Sun Feb 13, 2005 2:20 pm ] | ||
Post subject: | |||
u use -> to access the sub variables |
Author: | wtd [ Sun Feb 13, 2005 5:32 pm ] | ||
Post subject: | |||
Andy wrote:
u use -> to access the sub variables "members" Structs in C++ are simply classes where everything's public by default. |
Author: | Andy [ Sun Feb 13, 2005 7:52 pm ] |
Post subject: | |
o oops.. it doesnt work on structs? hmmmm heh use classes |
Author: | md [ Sun Feb 13, 2005 8:30 pm ] |
Post subject: | |
no no, -> works on structs in C++, but structs are really just classes where everything is public |
Author: | Mazer [ Mon Feb 14, 2005 10:13 am ] |
Post subject: | |
By default. You can still make members protected/private, right? |
Author: | wtd [ Mon Feb 14, 2005 12:36 pm ] |
Post subject: | |
Coutsos wrote: By default. You can still make members protected/private, right?
Absolutely. You can subclass structs, too. |
Author: | omni [ Mon Feb 14, 2005 7:58 pm ] |
Post subject: | |
If I want to create an array of strings, would I just make a 2d array? |
Author: | Andy [ Mon Feb 14, 2005 8:01 pm ] |
Post subject: | |
didnt u learn the string class??? u guys didnt do classes??? |
Author: | omni [ Mon Feb 14, 2005 8:09 pm ] |
Post subject: | |
I'm self teaching myself this. So there is no "guys" And no I didn't learn string classes or classes in general : / |
Author: | Andy [ Mon Feb 14, 2005 8:14 pm ] |
Post subject: | |
windsor eh? do u go to massey? anyways, you should read up on classes, they are very very useful |
Author: | Mazer [ Mon Feb 14, 2005 8:18 pm ] |
Post 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). |
Author: | omni [ Mon Feb 14, 2005 8:20 pm ] |
Post 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. |
Author: | Mazer [ Mon Feb 14, 2005 8:33 pm ] |
Post 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? |
Author: | Andy [ Mon Feb 14, 2005 10:17 pm ] |
Post subject: | |
system("cls") u need to include window.h or was it windows.h w.e try both |
Author: | wtd [ Tue Feb 15, 2005 12:00 am ] | ||
Post subject: | |||
Coutsos wrote: 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?
Indeed, if you do that, how does the user copy and paste output from your program? If they forget to do so and the program clears the screen, that output is gone. Or if it just scrolls the page several lines to mimic clearing the screen the user has a bunch of useless blank lines. As for an array of strings, don't use C-strings (character arrays) or arrays. Instead use a vector of std::string objects.
Vectors are beautiful because they permit resizing and keep track of their own size. |