something with struct and class i dont understand

you know you create a struct like this right...

1
2
3
4
5
6
struct weasels
{
int num;
weasels *link;

}*p;   /// what the hell is this????? 



what is that between the } and the ; is it a weasel *p????


hey i totally meant to put this in the beginners section
Last edited on
it defines a pointer p to weasels as a gobal variable.
That statement defines a variable called p. The variable of type struct weasels*.

In other words, that statement is doing the following things:

1) Defining a structure type weasels, with data members num and link

2) Declaring a variable called p, whose type is a pointer to that structure.
what about new as well



1
2
weasels *root;
root = new weasels;




i have no idea what i have done to root or weasels, the tutorials dont make sence to me
this struct should help you understand how structs works:

1
2
3
4
5
6
struct color
{
   int R;
   int G;
   int B;
}red, green, blue, orange, purple;


you define a new datatype "color" and you create variables with this datatype "red, green,..."

the * simply tells you it's a pointer. so just like int *i is an integer pointer, in your case *p is a pointer of the datatype weasels.
what about new as well
1
2
weasels *root;
root = new weasels;

i have no idea what i have done to root or weasels, the tutorials dont make sence to me

This is such basic stuff, that you should probably be posting in the Beginners forum.

weasels *root;

means that you are declaring a variable called root, whose type is a pointer to weasels.

root = new weasels;

means that you are dynamically allocating some memory to store an object of type weasels, and you are setting the value of the variable root to be the address of the start of the memory you're allocating.
yeah i said that i ment to post there sry.
yeah i said that i ment to post there sry


Oh, sorry - I somehow missed that the first time I read your post.

Did my answer help?
yes very much so, im almost able to completley grasp pointers and linked lists soon, plus its impossible to find a tut explaining new cos you cant search for the keyword :P
Topic archived. No new replies allowed.