
-----------------------------------
omni
Sat Feb 12, 2005 10:18 pm

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?

-----------------------------------
wtd
Sun Feb 13, 2005 12:21 am


-----------------------------------
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.

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
Sun Feb 13, 2005 2:20 pm


-----------------------------------

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
Sun Feb 13, 2005 5:32 pm


-----------------------------------

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
Sun Feb 13, 2005 7:52 pm


-----------------------------------
o oops.. it doesnt work on structs? :oops:  hmmmm heh use classes  :roll:

-----------------------------------
md
Sun Feb 13, 2005 8:30 pm


-----------------------------------
no no, -> works on structs in C++, but structs are really just classes where everything is public

-----------------------------------
Mazer
Mon Feb 14, 2005 10:13 am


-----------------------------------
By default. You can still make members protected/private, right?

-----------------------------------
wtd
Mon Feb 14, 2005 12:36 pm


-----------------------------------
By default. You can still make members protected/private, right?

Absolutely.

You can subclass structs, too.

-----------------------------------
omni
Mon Feb 14, 2005 7:58 pm


-----------------------------------
If I want to create an array of strings, would I just make a 2d array?

-----------------------------------
Andy
Mon Feb 14, 2005 8:01 pm


-----------------------------------
didnt u learn the string class??? u guys didnt do classes???

-----------------------------------
omni
Mon Feb 14, 2005 8:09 pm


-----------------------------------
I'm self teaching myself this.  So there is no "guys" And no I didn't learn string classes or classes in general : /

-----------------------------------
Andy
Mon Feb 14, 2005 8:14 pm


-----------------------------------
windsor eh? do u go to massey? anyways, you should read up on classes, they are very very useful

-----------------------------------
Mazer
Mon Feb 14, 2005 8:18 pm


-----------------------------------
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
Mon Feb 14, 2005 8:20 pm


-----------------------------------
@ 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
Mon Feb 14, 2005 8:33 pm


-----------------------------------
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?

-----------------------------------
Andy
Mon Feb 14, 2005 10:17 pm


-----------------------------------
system("cls")
u need to include window.h or was it windows.h w.e try both

-----------------------------------
wtd
Tue Feb 15, 2005 12:00 am


-----------------------------------
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.

#include 
#include 

int main()
{
  std::vector foo(20);
  // instead of: std::string foo[20];

  return 0;
}

Vectors are beautiful because they permit resizing and keep track of their own size.
