P
__PaTeR
These days i'm writing so much , now i was learning something about
lists... In my code i have a while loop that ends up only when the
NEXT member of the struct is equal to NULL; ( i'm just surfing in the
list )
Finding some good resource about structures has been really hard, for
example, i found out by myself the difference between the 2 operators -
As i've seen through gdb and experiments, is that: when you declare a
struct this way:
struct lista a;
To access the members u have to use the dot, and "a" doesn't contain
the pointer to the memory, but the memory itself, just like an
integer.
Besides, if u declare it this way:
struct lista *q; /* = malloc... etc */
q is just a pointer to a memory region, just like a char definition
( char *a ). To access members you have to use the arrow -> .... don't
you?
SO: why this
struct lista *q = malloc(sizeof(struct lista));
q->num=n;
q->next = NULL;
works really good, but this one:
struct lista a;
a.num = n;
a.next = NULL;
does not?? This last code let my while ends never! And i ould the
reason, look at this gdb output:
-----------------------
(gdb) print a.next
$2 = (struct lista *) 0x0
(gdb) print a
$3 = {num = 1, next = 0x0}
(gdb) print &a
$4 = (struct lista *) 0xbfb4000c
(gdb) x $4
0xbfb4000c: 0x00000001
(gdb) x $4+4
0xbfb4002c: 0x08048b28
(gdb) print $4+4
$5 = (struct lista *) 0xbfb4002c <---- 1
(gdb) x $5
0xbfb4002c: 0x08048b28 <------ 2
(gdb) print $5
$6 = (struct lista *) 0xbfb4002c <----- 1
(gdb) x $6
0xbfb4002c: 0x08048b28 <------- 2
(gdb) quit
lists... In my code i have a while loop that ends up only when the
NEXT member of the struct is equal to NULL; ( i'm just surfing in the
list )
Finding some good resource about structures has been really hard, for
example, i found out by myself the difference between the 2 operators -
and . ... or at least i think i found it out.
As i've seen through gdb and experiments, is that: when you declare a
struct this way:
struct lista a;
To access the members u have to use the dot, and "a" doesn't contain
the pointer to the memory, but the memory itself, just like an
integer.
Besides, if u declare it this way:
struct lista *q; /* = malloc... etc */
q is just a pointer to a memory region, just like a char definition
( char *a ). To access members you have to use the arrow -> .... don't
you?
SO: why this
struct lista *q = malloc(sizeof(struct lista));
q->num=n;
q->next = NULL;
works really good, but this one:
struct lista a;
a.num = n;
a.next = NULL;
does not?? This last code let my while ends never! And i ould the
reason, look at this gdb output:
-----------------------
(gdb) print a.next
$2 = (struct lista *) 0x0
(gdb) print a
$3 = {num = 1, next = 0x0}
(gdb) print &a
$4 = (struct lista *) 0xbfb4000c
(gdb) x $4
0xbfb4000c: 0x00000001
(gdb) x $4+4
0xbfb4002c: 0x08048b28
(gdb) print $4+4
$5 = (struct lista *) 0xbfb4002c <---- 1
(gdb) x $5
0xbfb4002c: 0x08048b28 <------ 2
(gdb) print $5
$6 = (struct lista *) 0xbfb4002c <----- 1
(gdb) x $6
0xbfb4002c: 0x08048b28 <------- 2
(gdb) quit