Learning Linked Lists, Code Attached, My understanding of code attached, Please confirm understanding 
Author Message
 Learning Linked Lists, Code Attached, My understanding of code attached, Please confirm understanding

Hello,

Can anyone please confirm my understanding of why this gets
an error (seg fault actually).  Please notice the last line of the code
below.

Now,

head.next = &f fails because... well because syntax says
        you use head.next -> whatever when you deal with
        struct pointers

but

head.next -> &f fails well because now it is dereferenced
        so you should give actual value not address

so we go with

head = &f because head is a pointer to type node and you
        assign addresses to pointers ... this explains why
        the book says to point head to first node in list
        using head = &f and the above two errors explain
        why you can't use the .next member of the head
        to do the pointing

My concern with all this is why you could not use head.next (or similar
statement) to point head to first node.  I know this is a real loose
understanding of this but I hope i'm on the right track.

Thank you for your time,

Jason Smith

CODE BELOW:
==============================================
        /* link list */

        struct node {
                        int number;
                        struct node * next;
                    };

        struct node * head;

        struct node  f;

        struct node  s;

        struct node  t;

        /* head.next = &f; */

        f.next = &s;

        s.next = &t;

        t.next = NULL;

        head -> next = &f;



Sat, 25 Jun 2005 14:12:16 GMT  
 Learning Linked Lists, Code Attached, My understanding of code attached, Please confirm understanding

Quote:

> Hello,

> Can anyone please confirm my understanding of why this gets
> an error (seg fault actually).  Please notice the last line of the code
> below.

Sure. In the future, however, it would be better to include a piece of
_compilable_ code rather than an out-of-context snippet.

...but that's a quibble (at least for now).

Quote:

> Now,

> head.next = &f fails because... well because syntax says
>         you use head.next -> whatever when you deal with
>         struct pointers

Not exactly. For one thing, the expression `head.next' makes no sense
(and shouldn't even compile), as `head' refers to a _pointer_ to an
object of type `struct node'.

Even assuming the correct syntax, i.e. `head->next', undefined behavior
is invoked as `head' has not been defined to point to anything.

Quote:

> but

> head.next -> &f fails well because now it is dereferenced
>         so you should give actual value not address
See above.

> so we go with

> head = &f because head is a pointer to type node and you
>         assign addresses to pointers ... this explains why
>         the book says to point head to first node in list
>         using head = &f and the above two errors explain
>         why you can't use the .next member of the head
>         to do the pointing

No, here you're assigning the address of `f', which, as `f' is defined
to be a `struct node' is of type `pointer to struct node' to `head',
which is perfectly legitimate.

Quote:

> My concern with all this is why you could not use head.next (or similar
> statement) to point head to first node.  I know this is a real loose
> understanding of this but I hope i'm on the right track.

Again, see above.

Quote:

> Thank you for your time,

> Jason Smith

> CODE BELOW:
> ==============================================
>         /* link list */

>         struct node {
>                         int number;
>                         struct node * next;
>                     };

>         struct node * head;

>         struct node  f;

>         struct node  s;

>         struct node  t;

>         /* head.next = &f; */

>         f.next = &s;

>         s.next = &t;

>         t.next = NULL;

>         head -> next = &f;

HTH,
--ag
--
Artie Gold  --  Austin, TX

"May you keep turning the pages. And may the book never end."



Sat, 25 Jun 2005 12:23:41 GMT  
 Learning Linked Lists, Code Attached, My understanding of code attached, Please confirm understanding
<posted & mailed>

Quote:

> Hello,

> Can anyone please confirm my understanding of why this gets
> an error (seg fault actually).  Please notice the last line of the code
> below.

> Now,

> head.next = &f fails because... well because syntax says
>         you use head.next -> whatever when you deal with
>         struct pointers

> but

> head.next -> &f fails well because now it is dereferenced
>         so you should give actual value not address

> so we go with

> head = &f because head is a pointer to type node and you
>         assign addresses to pointers ... this explains why
>         the book says to point head to first node in list
>         using head = &f and the above two errors explain
>         why you can't use the .next member of the head
>         to do the pointing

> My concern with all this is why you could not use head.next (or similar
> statement) to point head to first node.  I know this is a real loose
> understanding of this but I hope i'm on the right track.

> Thank you for your time,

> Jason Smith

> CODE BELOW:
> ==============================================
>         /* link list */

>         struct node {
>                         int number;
>                         struct node * next;
>                     };

>         struct node * head;

...

Quote:
>         head -> next = &f;

What did you expect? You never set head!

--
Replace spamtrap with bd to reply.
All things that are, are with more spirit chased than enjoyed.
                -- Shakespeare, "Merchant of Venice"



Mon, 27 Jun 2005 09:07:23 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Help!(long source code attached)

2. FORGOT TO ATTACH SOURCE CODE!!!

3. FORGOT TO ATTACH SOURCE CODE!!!

4. CODE ATTACHED

5. C obfuscator (Obfuscator code attached)

6. install a modem attached to serial port in code

7. CSplitterWnd And CListView problem (sample code attached)

8. STL slow performance - code attached

9. Q: How can I use in code icon attached by /win32icon:ICON

10. C Experts: Please help with link list coding

11. Help on my C code, dont understand how to fix my compiler errors (PLEASE HELP)

12. Connecting to linked (attached) tables via ADO

 

 
Powered by phpBB® Forum Software