as in:
something->new
thanks and happy new year!
thanks
Printable View
as in:
something->new
thanks and happy new year!
thanks
For C....
If you have a structure called mystruct with a member called var, you would access var with mystruct.var.
If you had a pointer to the structure, you would use mystruct->var
Buy a good book about C, for example "The C Programming Language" and learn some C, or read some reference.
klausi
-PreludeCode:struct FOO
{
char *stuff = "floobie";
};
int main(void)
{
struct FOO foo;
struct FOO *pFoo;
puts(foo.stuff); /*This will work, prints floobie*/
puts(pFoo->stuff); /*okay here too, prints floobie*/
puts(foo->stuff);
/*bzzzzt, wrong answer!
**foo is a direct instance, not a pointer
**-> is used to with a pointer to a struct
*/
puts(pFoo.stuff);
/*bzzzzt, also wrong!
**pFoo is a pointer, treat it like one
** the . operator is used for a direct instance only
*/
return EXIT_SUCCESS;
}
I'll make it even easier to understand:
some_struct->element is exactly the same as saying (*some_struct).element
But who wants to dereference a struct just to access its elements? It is a shortcut, like i++ or x |= 7.
a helpful aspect of this deferencing is that passing a pointer to a user defined conglomerate type as an argument to a function is a lot less expensive on the stack than passing the whole instance of the type...
I had been wondering the same thing. Thanks for the answer.
master5001 I have never seen x | = 7 before. What does the |= do?
x |= 7
It's like saying x = x | 7
the | symbol is the bitwsie OR.
It basically works like this...let's say you have two numbers, 2 and 1
0000000010 (binary 2)
0000000001 (binary 1)
well when you bitwise OR them, it comes out like this:
which is 3Code:0000000010
| (OR)
0000000001
---------------------
0000000011
see what it did? Every there was a 1 in a place that there wasn't one in the other number, it put a 1 there. You can basically think of it as adding or combining numbers in binary (but only think of it like that in binary terms, because 6 | 6 != 12, that 2 | 1 = 3 is just sorta a conincidence.
thanks Ken
That is easy enough to understand.
It means "go through".
So if you have a pointer to and structure named 'bunnywabbit'. You can "go through" it (called indirection) to access it's fields.
[code]
typedef struct
{
int ears;
int feet;
int whiskers;
int tail;
}wabbit;
wabbit *bunnywabbit;
bunnywabbit = (wabbit*)malloc(sizeof(wabbit));
bunnywabbit->ears = 2;
bunnywabbit->feet = 4;
bunnywabbit->whiskers = 6;
bunnywabbit->tail = 1;
[\code]
enjoy.
Try code tag the other way (forgive my sorry old head)--
enjoy.Code:
typedef struct
{
int ears;
int feet;
int whiskers;
int tail;
}wabbit;
wabbit *bunnywabbit;
bunnywabbit = (wabbit*)malloc(sizeof(wabbit));
bunnywabbit->ears = 2;
bunnywabbit->feet = 4;
bunnywabbit->whiskers = 6;
bunnywabbit->tail = 1;