Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #87369 > unrolled thread

trying a linked list

Started by"Bill Cunningham" <nospam@nspam.invalid>
First post2016-05-07 17:06 -0400
Last post2016-05-09 13:28 -0700
Articles 20 on this page of 67 — 13 participants

Back to article view | Back to comp.lang.c


Contents

  trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-07 17:06 -0400
    Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-07 17:32 -0400
      Re: trying a linked list Rosario19 <Ros@invalid.invalid> - 2016-05-07 23:44 +0200
      Re: trying a linked list "martin.ambuhl" <mambuhl@earthlink.net> - 2016-05-07 19:49 -0400
        Re: trying a linked list Richard Heathfield <rjh@cpax.org.uk> - 2016-05-08 07:46 +0100
    Re: trying a linked list Keith Thompson <kst-u@mib.org> - 2016-05-07 14:35 -0700
      Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-07 17:53 -0400
        Re: trying a linked list Keith Thompson <kst-u@mib.org> - 2016-05-07 15:01 -0700
          Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-07 19:18 -0400
            Re: trying a linked list Jerry Stuckle <jstucklex@attglobal.net> - 2016-05-07 19:28 -0400
            Re: trying a linked list Keith Thompson <kst-u@mib.org> - 2016-05-07 16:40 -0700
            Re: trying a linked list Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-05-07 17:15 -0700
              Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-07 20:25 -0400
                Re: trying a linked list Keith Thompson <kst-u@mib.org> - 2016-05-07 20:10 -0700
                  Re: trying a linked list Richard Heathfield <rjh@cpax.org.uk> - 2016-05-08 07:53 +0100
                    Re: trying a linked list Keith Thompson <kst-u@mib.org> - 2016-05-08 14:19 -0700
                  Re: trying a linked list Manfred <mx2927@gmail.com> - 2016-05-08 18:45 +0200
                    Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-08 17:21 -0400
                  Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-08 14:38 -0400
                    Re: trying a linked list Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-05-08 20:08 +0100
                      Re: trying a linked list Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-05-08 20:26 +0100
                        Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-08 17:22 -0400
                      Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-08 15:28 -0400
                        Re: trying a linked list Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-05-08 20:36 +0100
                          Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-08 17:17 -0400
                            Re: trying a linked list Keith Thompson <kst-u@mib.org> - 2016-05-08 14:56 -0700
                              Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-08 18:17 -0400
                                Re: trying a linked list Keith Thompson <kst-u@mib.org> - 2016-05-08 19:31 -0700
                                  Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-09 14:59 -0400
                            Re: trying a linked list Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-05-08 23:22 +0100
                              Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-09 14:46 -0400
                        Re: trying a linked list Keith Thompson <kst-u@mib.org> - 2016-05-08 14:32 -0700
                          Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-08 18:05 -0400
                    Re: trying a linked list Keith Thompson <kst-u@mib.org> - 2016-05-08 14:26 -0700
                      Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-09 13:50 -0400
                        Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-09 14:08 -0400
                        Re: trying a linked list Keith Thompson <kst-u@mib.org> - 2016-05-09 11:38 -0700
                          Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-09 14:50 -0400
                            Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-09 14:51 -0400
                              Re: trying a linked list Keith Thompson <kst-u@mib.org> - 2016-05-09 13:51 -0700
                              Re: trying a linked list Ken Brody <kenbrody@spamcop.net> - 2016-05-09 17:53 -0400
                            Re: trying a linked list Keith Thompson <kst-u@mib.org> - 2016-05-09 13:47 -0700
                              Re: trying a linked list "Osmium" <r124c4u102@comcast.net> - 2016-05-09 15:58 -0500
                              Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-09 17:13 -0400
                                Re: trying a linked list Keith Thompson <kst-u@mib.org> - 2016-05-09 14:35 -0700
                                  Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-09 17:40 -0400
                                  Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-09 17:41 -0400
                                    Re: trying a linked list Keith Thompson <kst-u@mib.org> - 2016-05-09 15:10 -0700
                                      Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-09 22:02 -0400
                                        Re: trying a linked list Keith Thompson <kst-u@mib.org> - 2016-05-09 21:17 -0700
                                      Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-09 22:31 -0400
              Re: trying a linked list "Bill Cunningham" <nospam@nspam.invalid> - 2016-05-07 20:30 -0400
              Re: trying a linked list Keith Thompson <kst-u@mib.org> - 2016-05-07 20:06 -0700
              Re: trying a linked list Richard Heathfield <rjh@cpax.org.uk> - 2016-05-08 07:50 +0100
                Re: trying a linked list BartC <bc@freeuk.com> - 2016-05-08 10:33 +0100
                  Re: trying a linked list Richard Heathfield <rjh@cpax.org.uk> - 2016-05-08 10:39 +0100
                    Re: trying a linked list BartC <bc@freeuk.com> - 2016-05-08 11:14 +0100
                      Re: trying a linked list Richard Heathfield <rjh@cpax.org.uk> - 2016-05-08 13:34 +0100
                        Re: trying a linked list BartC <bc@freeuk.com> - 2016-05-08 14:39 +0100
                          Re: trying a linked list Richard Heathfield <rjh@cpax.org.uk> - 2016-05-08 14:51 +0100
                        Re: trying a linked list Siri Cruise <chine.bleu@yahoo.com> - 2016-05-08 07:30 -0700
                Re: trying a linked list Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-05-08 10:36 +0100
                  Re: trying a linked list Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-05-08 02:49 -0700
                    Re: trying a linked list Keith Thompson <kst-u@mib.org> - 2016-05-08 14:17 -0700
                      Re: trying a linked list Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-05-08 14:36 -0700
                        Re: trying a linked list Keith Thompson <kst-u@mib.org> - 2016-05-08 14:57 -0700
                          Re: trying a linked list Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-05-09 13:28 -0700

Page 2 of 4 — ← Prev page 1 [2] 3 4  Next page →


#87423

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-05-08 20:26 +0100
Message-ID<8760uogxld.fsf@bsb.me.uk>
In reply to#87421
ram@zedat.fu-berlin.de (Stefan Ram) writes:

> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>You might try changing it to use malloc rather than declaring all four
>>nodes in main.
>
>   Malloc opens a whole new can of worms for beginners.
>   So I think it's a good idea to use automatic storage
>   duration as long as possible, when one is still learning.

Except that you get stuck on utility -- linked lists (et al) are really
only useful when the storage is not automatic.  I can think of
counterexamples but they would all be described, at best, as tricksy.
(You can use recursion and a linked linked list of automatic objects to
do various things, but even then there are probably better methods.)

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#87435

From"Bill Cunningham" <nospam@nspam.invalid>
Date2016-05-08 17:22 -0400
Message-ID<ngoagg$kdc$1@dont-email.me>
In reply to#87423
"Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message 
news:8760uogxld.fsf@bsb.me.uk...

> Except that you get stuck on utility -- linked lists (et al) are really
> only useful when the storage is not automatic.  I can think of
> counterexamples but they would all be described, at best, as tricksy.
> (You can use recursion and a linked linked list of automatic objects to
> do various things, but even then there are probably better methods.)

    I don't know what you're saying but it sounds important. I wish I knew 
what you were saying.

Bill

[toc] | [prev] | [next] | [standalone]


#87424

From"Bill Cunningham" <nospam@nspam.invalid>
Date2016-05-08 15:28 -0400
Message-ID<ngo3q2$r9a$1@dont-email.me>
In reply to#87421
"Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message 
news:87d1owgyev.fsf@bsb.me.uk...

...
>  #include <stdio.h>
>
>  struct node {
>      int number;
>      struct node *next;
>  };
>
>  int main(void)
>  {
>       struct node n4 = {4, NULL},
>                   n3 = {3, &n4},
>                   n2 = {2, &n3},
>                   n1 = {1, &n2};

    This above is very intriguing code. If I can decypher it it looks like 
n4 is of type "struct node" Now the members of the struct {4,NULL} that I've 
never seen. don't member have to have there own types? Or are there some 
kind of defaults? And the alias's have a value too that is different. Hum 
that's new to me. I can't really decypher it.

>       for (struct node *np = &n1; np != NULL; np = np->next)
>            printf("%d\n", np->number);
>  }
>
> You might try changing it to use malloc rather than declaring all four
> nodes in main.

[toc] | [prev] | [next] | [standalone]


#87427

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-05-08 20:36 +0100
Message-ID<87twi8fikb.fsf@bsb.me.uk>
In reply to#87424
"Bill Cunningham" <nospam@nspam.invalid> writes:

> "Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message 
> news:87d1owgyev.fsf@bsb.me.uk...
>
> ...
>>  #include <stdio.h>
>>
>>  struct node {
>>      int number;
>>      struct node *next;
>>  };
>>
>>  int main(void)
>>  {
>>       struct node n4 = {4, NULL},
>>                   n3 = {3, &n4},
>>                   n2 = {2, &n3},
>>                   n1 = {1, &n2};
>
>     This above is very intriguing code. If I can decypher it it looks like 
> n4 is of type "struct node" Now the members of the struct {4,NULL} that I've 
> never seen.

You need to read a book about C then.  I can't imagine any book about C
that does not show a struct object being initialised.

> don't member have to have there own types?

Yes, the types are int (for the first member) and struct node * (for the
second member).  That's why {4, NULL} is a reasonable initialiser.

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#87432

From"Bill Cunningham" <nospam@nspam.invalid>
Date2016-05-08 17:17 -0400
Message-ID<ngoa7k$jg5$1@dont-email.me>
In reply to#87427
"Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message 
news:87twi8fikb.fsf@bsb.me.uk...
> "Bill Cunningham" <nospam@nspam.invalid> writes:
>
>> "Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message
>> news:87d1owgyev.fsf@bsb.me.uk...
>>
>> ...
>>>  #include <stdio.h>
>>>
>>>  struct node {
>>>      int number;
>>>      struct node *next;
>>>  };
>>>
>>>  int main(void)
>>>  {
>>>       struct node n4 = {4, NULL},
>>>                   n3 = {3, &n4},
>>>                   n2 = {2, &n3},
>>>                   n1 = {1, &n2};
>>
>>     This above is very intriguing code. If I can decypher it it looks 
>> like
>> n4 is of type "struct node" Now the members of the struct {4,NULL} that 
>> I've
>> never seen.
>
> You need to read a book about C then.  I can't imagine any book about C
> that does not show a struct object being initialised.
>
>> don't member have to have there own types?
>
> Yes, the types are int (for the first member) and struct node * (for the
> second member).  That's why {4, NULL} is a reasonable initialiser.

    I've never seen a struct initialized with members and no explicit 
declaration of types ergo,

struct node{
    int number;
Where you have,

struct node {number}; //For example as compared to what I'm doing. I know 
your calling yours n4.

I erased and rewrote code up to where there's an error,
#include <stdio.h>
#include <stdlib.h>

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


int main()
{
    struct node base;
    struct node *sp;
    sp = &base;
    sp->number = 1;
    printf("%d\n", *sp);
    sp->next = NULL;
    sp = malloc(sizeof *sp);
    sp->next->number = 2;
    printf("%d\n", *sp);
}

I'm not quite sure why the malloc isn't working as others have told me to 
use and what I think I'm reading. And I did try this and got an error,

struct node{
    int number=0;
...

The compiler didn't like it. I don't know if it's C99 or C11. It has 
inittypes.h and complex.h and stdint.h so C11 probably.

So how is your method different?

Bill


[toc] | [prev] | [next] | [standalone]


#87442

FromKeith Thompson <kst-u@mib.org>
Date2016-05-08 14:56 -0700
Message-ID<lnlh3kw6w4.fsf@kst-u.example.com>
In reply to#87432
"Bill Cunningham" <nospam@nspam.invalid> writes:
> "Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message 
> news:87twi8fikb.fsf@bsb.me.uk...
>> "Bill Cunningham" <nospam@nspam.invalid> writes:
>>> "Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message
>>> news:87d1owgyev.fsf@bsb.me.uk...
[...]
>>>>  int main(void)
>>>>  {
>>>>       struct node n4 = {4, NULL},
>>>>                   n3 = {3, &n4},
>>>>                   n2 = {2, &n3},
>>>>                   n1 = {1, &n2};
>>>
>>>     This above is very intriguing code. If I can decypher it it
>>> looks like n4 is of type "struct node" Now the members of the struct
>>> {4,NULL} that I've never seen.
>>
>> You need to read a book about C then.  I can't imagine any book about C
>> that does not show a struct object being initialised.
>>
>>> don't member have to have there own types?
>>
>> Yes, the types are int (for the first member) and struct node * (for the
>> second member).  That's why {4, NULL} is a reasonable initialiser.
>
>     I've never seen a struct initialized with members and no explicit 
> declaration of types ergo,
>
> struct node{
>     int number;
> Where you have,
>
> struct node {number}; //For example as compared to what I'm doing. I know 
> your calling yours n4.

You've confused a struct *definition* with a struct object
*initialization*.

This:
    struct node {
        int number;
        struct node *next;
    };
defines the type "struct node".  It specifies the names and types of its
members.

This:
    struct node n4 = { 4, NULL };
defines an object "n4"; the "{ 4, NULL }" part is an *initializer*.
Initializers don't need to specify the types of the members; that's
already specified by the struct definition.


> I erased and rewrote code up to where there's an error,
> #include <stdio.h>
> #include <stdlib.h>
>
> struct node {
>     int number;
>     struct node *next;
> };
>
>
> int main()
> {
>     struct node base;
>     struct node *sp;
>     sp = &base;
>     sp->number = 1;
>     printf("%d\n", *sp);

As I discussed in another followup, the above is wrong.

>     sp->next = NULL;
>     sp = malloc(sizeof *sp);
>     sp->next->number = 2;
>     printf("%d\n", *sp);
> }
>
> I'm not quite sure why the malloc isn't working as others have told me to 
> use and what I think I'm reading.

The malloc() should have worked just fine.  What makes you think it
didn't?  It allocated space for a new "struct node" object that sp then
points to (though you didn't check that the allocation succeeded).  But,
as I've also mentioned, sp->next is uninitialized, which is why
    sp->next->number = 2;
is likely to blow up in your face.

>                                   And I did try this and got an error,
>
> struct node{
>     int number=0;
> ...
>
> The compiler didn't like it. I don't know if it's C99 or C11. It has 
> inittypes.h and complex.h and stdint.h so C11 probably.

You cannot specify initial values in a struct definition.  That's true
for alll versions of C.  (It would be nice if you could, but it happens
that that's not a feature of the language.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#87445

From"Bill Cunningham" <nospam@nspam.invalid>
Date2016-05-08 18:17 -0400
Message-ID<ngodnh$2go$1@dont-email.me>
In reply to#87442
"Keith Thompson" <kst-u@mib.org> wrote in message 
news:lnlh3kw6w4.fsf@kst-u.example.com...
> "Bill Cunningham" <nospam@nspam.invalid> writes:
>> "Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message
>> news:87twi8fikb.fsf@bsb.me.uk...
>>> "Bill Cunningham" <nospam@nspam.invalid> writes:
>>>> "Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message
>>>> news:87d1owgyev.fsf@bsb.me.uk...
> [...]
>>>>>  int main(void)
>>>>>  {
>>>>>       struct node n4 = {4, NULL},
>>>>>                   n3 = {3, &n4},
>>>>>                   n2 = {2, &n3},
>>>>>                   n1 = {1, &n2};
>>>>
>>>>     This above is very intriguing code. If I can decypher it it
>>>> looks like n4 is of type "struct node" Now the members of the struct
>>>> {4,NULL} that I've never seen.
>>>
>>> You need to read a book about C then.  I can't imagine any book about C
>>> that does not show a struct object being initialised.
>>>
>>>> don't member have to have there own types?
>>>
>>> Yes, the types are int (for the first member) and struct node * (for the
>>> second member).  That's why {4, NULL} is a reasonable initialiser.
>>
>>     I've never seen a struct initialized with members and no explicit
>> declaration of types ergo,
>>
>> struct node{
>>     int number;
>> Where you have,
>>
>> struct node {number}; //For example as compared to what I'm doing. I know
>> your calling yours n4.
>
> You've confused a struct *definition* with a struct object
> *initialization*.
>
> This:
>    struct node {
>        int number;
>        struct node *next;
>    };
> defines the type "struct node".  It specifies the names and types of its
> members.
>
> This:
>    struct node n4 = { 4, NULL };
> defines an object "n4"; the "{ 4, NULL }" part is an *initializer*.
> Initializers don't need to specify the types of the members; that's
> already specified by the struct definition.
>
>
>> I erased and rewrote code up to where there's an error,
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> struct node {
>>     int number;
>>     struct node *next;
>> };
>>
>>
>> int main()
>> {
>>     struct node base;
>>     struct node *sp;
>>     sp = &base;
>>     sp->number = 1;
>>     printf("%d\n", *sp);
>
> As I discussed in another followup, the above is wrong.
>
>>     sp->next = NULL;
>>     sp = malloc(sizeof *sp);
>>     sp->next->number = 2;
>>     printf("%d\n", *sp);
>> }
>>
>> I'm not quite sure why the malloc isn't working as others have told me to
>> use and what I think I'm reading.
>
> The malloc() should have worked just fine.  What makes you think it
> didn't?  It allocated space for a new "struct node" object that sp then
> points to (though you didn't check that the allocation succeeded).  But,
> as I've also mentioned, sp->next is uninitialized, which is why
>    sp->next->number = 2;
> is likely to blow up in your face.
>
>>                                   And I did try this and got an error,
>>
>> struct node{
>>     int number=0;
>> ...
>>
>> The compiler didn't like it. I don't know if it's C99 or C11. It has
>> inittypes.h and complex.h and stdint.h so C11 probably.
>
> You cannot specify initial values in a struct definition.  That's true
> for alll versions of C.  (It would be nice if you could, but it happens
> that that's not a feature of the language.)

    Thanks much Keith. I did not check to see if malloc worked or not. But I 
didn't think I'd have to set the pointer to NULL when I was going to use it. 
Does NULL "clear" it or something? Is that the problem with "dangling 
pointers"?

Bill

[toc] | [prev] | [next] | [standalone]


#87451

FromKeith Thompson <kst-u@mib.org>
Date2016-05-08 19:31 -0700
Message-ID<lnd1owvu5z.fsf@kst-u.example.com>
In reply to#87445
"Bill Cunningham" <nospam@nspam.invalid> writes:
[snip]
>     Thanks much Keith. I did not check to see if malloc worked or not. But I 
> didn't think I'd have to set the pointer to NULL when I was going to use it. 
> Does NULL "clear" it or something? Is that the problem with "dangling 
> pointers"?

A null pointer doesn't point to anything.  Yes, setting a pointer object
to NULL "clears" it.

An uninitialized pointer is garbage; attempting to use its value has
undefined behavior.

I'm not sure just what you mean by "I did not check to see if malloc
worked or not. But I didn't think I'd have to set the pointer to NULL
when I was going to use it."  If you're going to assign a value to a
pointer, there's no need to set it to NULL first:

    int *p = NULL;  /* This initialization isn't necessary */
    p = &something;

But a NULL pointer is used to mark the end of a linked list.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#87506

From"Bill Cunningham" <nospam@nspam.invalid>
Date2016-05-09 14:59 -0400
Message-ID<ngqmgm$51f$1@dont-email.me>
In reply to#87451
"Keith Thompson" <kst-u@mib.org> wrote in message 
news:lnd1owvu5z.fsf@kst-u.example.com...

> But a NULL pointer is used to mark the end of a linked list.

    Right.

This is as you understand I am sure hardly any real "linked list". I see 
value in the linked list concept though. It' worth pursuing. Perhaps that 
was a bad thread title. But what I believe I am actually trying to do is 
dereference a series of pointers to pointers. A very basic concept behind 
the linked list.

Bill



[toc] | [prev] | [next] | [standalone]


#87446

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-05-08 23:22 +0100
Message-ID<87d1owfawn.fsf@bsb.me.uk>
In reply to#87432
"Bill Cunningham" <nospam@nspam.invalid> writes:

> "Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message 
> news:87twi8fikb.fsf@bsb.me.uk...
>> "Bill Cunningham" <nospam@nspam.invalid> writes:
>>
>>> "Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message
>>> news:87d1owgyev.fsf@bsb.me.uk...
>>>
>>> ...
>>>>  #include <stdio.h>
>>>>
>>>>  struct node {
>>>>      int number;
>>>>      struct node *next;
>>>>  };
>>>>
>>>>  int main(void)
>>>>  {
>>>>       struct node n4 = {4, NULL},
>>>>                   n3 = {3, &n4},
>>>>                   n2 = {2, &n3},
>>>>                   n1 = {1, &n2};
>>>
>>>     This above is very intriguing code. If I can decypher it it looks 
>>> like
>>> n4 is of type "struct node" Now the members of the struct {4,NULL} that 
>>> I've
>>> never seen.
>>
>> You need to read a book about C then.  I can't imagine any book about C
>> that does not show a struct object being initialised.
>>
>>> don't member have to have there own types?
>>
>> Yes, the types are int (for the first member) and struct node * (for the
>> second member).  That's why {4, NULL} is a reasonable initialiser.
>
>     I've never seen a struct initialized with members and no explicit 
> declaration of types ergo,
>
> struct node{
>     int number;
> Where you have,
>
> struct node {number};

No I don't.  You will not be able to grasp what's going on if you can't
see what's written.  I, too, have the full struct node declaration on
lines 3 to 6, and nowhere do I have struct node {number}.

After declaring the fact that a struct node has twp members, one an int
called number and one a struct node * called next, I go on to declare
some examples of struct node objects called n4, n3, n2 and n1.  Each one
is initialisied using the very common = {...} syntax.

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#87501

From"Bill Cunningham" <nospam@nspam.invalid>
Date2016-05-09 14:46 -0400
Message-ID<ngqlof$27g$1@dont-email.me>
In reply to#87446
"Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message 
news:87d1owfawn.fsf@bsb.me.uk...

...

> No I don't.  You will not be able to grasp what's going on if you can't
> see what's written.  I, too, have the full struct node declaration on
> lines 3 to 6, and nowhere do I have struct node {number}.

    I didn't say *you* had number there. You have 4. I was pointing out a 
comparision to your "4" and *my* "number". I assume your "4" is taking my 
"number" place.

> After declaring the fact that a struct node has twp members, one an int
> called number and one a struct node * called next, I go on to declare
> some examples of struct node objects called n4, n3, n2 and n1.  Each one
> is initialisied using the very common = {...} syntax.

    I see I've never seen it that way before. But there's a lot I haven't 
seen. I'm not questioning anyone here. It's me that doesn't know what's 
going on. I'm looking at tutorials most of which go right over my head and I 
have to look at several to see their commonality. I have lately found some 
very enlightening ones. Written very well.

Bill

[toc] | [prev] | [next] | [standalone]


#87437

FromKeith Thompson <kst-u@mib.org>
Date2016-05-08 14:32 -0700
Message-ID<ln1t5cxml6.fsf@kst-u.example.com>
In reply to#87424
"Bill Cunningham" <nospam@nspam.invalid> writes:
> "Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message 
> news:87d1owgyev.fsf@bsb.me.uk...
>
> ...
>>  #include <stdio.h>
>>
>>  struct node {
>>      int number;
>>      struct node *next;
>>  };
>>
>>  int main(void)
>>  {
>>       struct node n4 = {4, NULL},
>>                   n3 = {3, &n4},
>>                   n2 = {2, &n3},
>>                   n1 = {1, &n2};
>
>     This above is very intriguing code. If I can decypher it it looks like 
> n4 is of type "struct node"

Yes.

>                             Now the members of the struct {4,NULL} that I've 
> never seen. don't member have to have there own types?

Of course they have their own types.  The "number" member has type int,
and the "next" member has type struct node*.  You don't need to specify
those types in the initializer.  The values in the initializer are
assigned to the members of the struct in the order in which those
members are defined: first "number", then "next".  You're almost
certainly seen that kind of thing before.

The 4-line declaration above can (and arguably should) be broken down
to:

    struct node n4 = {4, NULL},
    struct node n3 = {3, &n4};
    struct node n2 = {2, &n3};
    struct node n1 = {1, &n2};

The first line:
    struct node n4 = {4, NULL},
is essentially equivalent to:
    struct node n4;
    n4.number = 4;
    n4.next = NULL;
and similarly for the other three declarations.

This isn't how you'd actually set up a linked list in the real world,
but it's valid and shows the general principles.

>                                                        Or are there some 
> kind of defaults? And the alias's have a value too that is different.

I see no aliases.  What are you referring to?

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#87444

From"Bill Cunningham" <nospam@nspam.invalid>
Date2016-05-08 18:05 -0400
Message-ID<ngod16$vvt$1@dont-email.me>
In reply to#87437
"Keith Thompson" <kst-u@mib.org> wrote in message 
news:ln1t5cxml6.fsf@kst-u.example.com...
> "Bill Cunningham" <nospam@nspam.invalid> writes:
>> "Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message
>> news:87d1owgyev.fsf@bsb.me.uk...
>>
>> ...
>>>  #include <stdio.h>
>>>
>>>  struct node {
>>>      int number;
>>>      struct node *next;
>>>  };
>>>
>>>  int main(void)
>>>  {
>>>       struct node n4 = {4, NULL},
>>>                   n3 = {3, &n4},
>>>                   n2 = {2, &n3},
>>>                   n1 = {1, &n2};
>>
>>     This above is very intriguing code. If I can decypher it it looks 
>> like
>> n4 is of type "struct node"
>
> Yes.
>
>>                             Now the members of the struct {4,NULL} that 
>> I've
>> never seen. don't member have to have there own types?
>
> Of course they have their own types.  The "number" member has type int,
> and the "next" member has type struct node*.  You don't need to specify
> those types in the initializer.  The values in the initializer are
> assigned to the members of the struct in the order in which those
> members are defined: first "number", then "next".  You're almost
> certainly seen that kind of thing before.
>
> The 4-line declaration above can (and arguably should) be broken down
> to:
>
>    struct node n4 = {4, NULL},
>    struct node n3 = {3, &n4};
>    struct node n2 = {2, &n3};
>    struct node n1 = {1, &n2};
>
> The first line:
>    struct node n4 = {4, NULL},
> is essentially equivalent to:
>    struct node n4;
>    n4.number = 4;
>    n4.next = NULL;
> and similarly for the other three declarations.
>
> This isn't how you'd actually set up a linked list in the real world,
> but it's valid and shows the general principles.
>
>>                                                        Or are there some
>> kind of defaults? And the alias's have a value too that is different.
>
> I see no aliases.  What are you referring to?
>
> [...]

cut

 struct node n4 = {4, NULL},
>>>                   n3 = {3, &n4},
>>>                   n2 = {2, &n3},
>>>                   n1 = {1, &n2};
>>

cut

I didn't see the semicolon after the {4,NULL} but I see now there are 4 
structs here. That would rid me of my memory problem. I am not using malloc 
right or it's not what to use. And others are doing it so there must be a 
way to do it right.

Bill

[toc] | [prev] | [next] | [standalone]


#87436

FromKeith Thompson <kst-u@mib.org>
Date2016-05-08 14:26 -0700
Message-ID<ln60uoxmug.fsf@kst-u.example.com>
In reply to#87417
"Bill Cunningham" <nospam@nspam.invalid> writes:
> "Keith Thompson" <kst-u@mib.org> wrote in message 
> news:lnzis1xn0v.fsf@kst-u.example.com...
>
> [...]
>
>> Linked lists are normally allocated one node at a time, with each node
>> being allocated as it's needed.
>
>     Yes I know that 's there purpose. My goal is to get past,
>
> 1
> segmentation fault; to
>
> 1
> 2
> 3
> 4
>
> And I'll be a success. I posted these int casts like you suggested and got 
> these errors.

I did *not* suggest casting anything.  I pointed out (incorrectly, as
it turns out) that casting to int would avoid the compile-time error,
but I specifically said that's not what you should do.

> p.c: In function 'main':
> p.c:15:5: error: aggregate value used where an integer was expected
>      printf("%d\n", (int)*sp);
>      ^
> p.c:18:5: error: aggregate value used where an integer was expected
>      printf("%d\n", (int)*sp);
>      ^

(That error message is poorly phrased.  The operand of the (int) cast is
expected to be of scalar type; it needn't be an integer.  But that
doesn't matter much.)

> I looked up trying to use malloc and they say never cast it's return. Here's 
> the code thus far,
>
> #include <stdio.h>
> #include <stdlib.h>
>
> struct node {
>     int number;
>     struct node *next;
> };
>
> int main()

A very minor quibble: This is better written as "int main(void)".

> {
>     struct node base;
>     struct node *sp;
>     sp = &base;
>     sp->number = 1;
>     printf("%d\n", *sp);

This is the same mistake you made before.

"%d" requires an argument of type int.  What is the type of *sp?
What do you expect this printf call to do?

There is no point in going further until you have answers to the above
questions and an idea of how to fix the problem.

>     sp = malloc(sizeof *sp);
>     sp->next->number = 2;

sp->next is an uninitialized pointer.

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#87493

From"Bill Cunningham" <nospam@nspam.invalid>
Date2016-05-09 13:50 -0400
Message-ID<ngqif8$kn9$1@dont-email.me>
In reply to#87436
"Keith Thompson" <kst-u@mib.org> wrote in message 
news:ln60uoxmug.fsf@kst-u.example.com...
> "Bill Cunningham" <nospam@nspam.invalid> writes:
>> "Keith Thompson" <kst-u@mib.org> wrote in message
>> news:lnzis1xn0v.fsf@kst-u.example.com...
>>
>> [...]
>>
>>> Linked lists are normally allocated one node at a time, with each node
>>> being allocated as it's needed.
>>
>>     Yes I know that 's there purpose. My goal is to get past,
>>
>> 1
>> segmentation fault; to
>>
>> 1
>> 2
>> 3
>> 4
>>
>> And I'll be a success. I posted these int casts like you suggested and 
>> got
>> these errors.
>
> I did *not* suggest casting anything.  I pointed out (incorrectly, as
> it turns out) that casting to int would avoid the compile-time error,
> but I specifically said that's not what you should do.
>
>> p.c: In function 'main':
>> p.c:15:5: error: aggregate value used where an integer was expected
>>      printf("%d\n", (int)*sp);
>>      ^
>> p.c:18:5: error: aggregate value used where an integer was expected
>>      printf("%d\n", (int)*sp);
>>      ^
>
> (That error message is poorly phrased.  The operand of the (int) cast is
> expected to be of scalar type; it needn't be an integer.  But that
> doesn't matter much.)
>
>> I looked up trying to use malloc and they say never cast it's return. 
>> Here's
>> the code thus far,
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> struct node {
>>     int number;
>>     struct node *next;
>> };
>>
>> int main()
>
> A very minor quibble: This is better written as "int main(void)".
>
>> {
>>     struct node base;
>>     struct node *sp;
>>     sp = &base;
>>     sp->number = 1;
>>     printf("%d\n", *sp);
>
> This is the same mistake you made before.
>
> "%d" requires an argument of type int.  What is the type of *sp?
> What do you expect this printf call to do?

    IDK but one thing I /can't/ do is cast an int to an agreggate value, 
like this,

printf("%d\n", (int) *sp);

Thanks out. And putting NULL in the struct doesn't work either. So I'm at a 
loss here. Here's the latest code.

 #include <stdio.h>
#include <stdlib.h>

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


int main()
{
    struct node base;
    struct node *sp;
    sp = &base;
    sp->number = 1;
    printf("%d\n", *sp);
    sp->next = NULL;  //initialize to NULL as I can see
    sp = malloc(sizeof *sp);
    sp->next->number = 2;  //No int casts here.
    printf("%d\n", *sp);
}

[toc] | [prev] | [next] | [standalone]


#87496

From"Bill Cunningham" <nospam@nspam.invalid>
Date2016-05-09 14:08 -0400
Message-ID<ngqjgi$ot5$1@dont-email.me>
In reply to#87493
"Bill Cunningham" <nospam@nspam.invalid> wrote in message 
news:ngqif8$kn9$1@dont-email.me...
>
> "Keith Thompson" <kst-u@mib.org> wrote in message 
> news:ln60uoxmug.fsf@kst-u.example.com...
>> "Bill Cunningham" <nospam@nspam.invalid> writes:
>>> "Keith Thompson" <kst-u@mib.org> wrote in message
>>> news:lnzis1xn0v.fsf@kst-u.example.com...

...

The debugger says that the segmentation fault occurs at this line,

sp->next->number=2;

Allocation of memory is not occuring at the right place or not occuring. I 
haven't tested malloc I will try that. I will have to look somethings up or 
grasp at straws.

Bill

[toc] | [prev] | [next] | [standalone]


#87500

FromKeith Thompson <kst-u@mib.org>
Date2016-05-09 11:38 -0700
Message-ID<lninynuldz.fsf@kst-u.example.com>
In reply to#87493
"Bill Cunningham" <nospam@nspam.invalid> writes:
> "Keith Thompson" <kst-u@mib.org> wrote in message 
> news:ln60uoxmug.fsf@kst-u.example.com...
>> "Bill Cunningham" <nospam@nspam.invalid> writes:
[...]
>>> {
>>>     struct node base;
>>>     struct node *sp;
>>>     sp = &base;
>>>     sp->number = 1;
>>>     printf("%d\n", *sp);
>>
>> This is the same mistake you made before.
>>
>> "%d" requires an argument of type int.  What is the type of *sp?
>> What do you expect this printf call to do?
>
>     IDK but
[...]

Then my advice is to stop and don't attempt anything else until you can
answer those questions.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#87502

From"Bill Cunningham" <nospam@nspam.invalid>
Date2016-05-09 14:50 -0400
Message-ID<ngqlud$2vo$1@dont-email.me>
In reply to#87500
"Keith Thompson" <kst-u@mib.org> wrote in message 
news:lninynuldz.fsf@kst-u.example.com...
> "Bill Cunningham" <nospam@nspam.invalid> writes:
>> "Keith Thompson" <kst-u@mib.org> wrote in message
>> news:ln60uoxmug.fsf@kst-u.example.com...
>>> "Bill Cunningham" <nospam@nspam.invalid> writes:
> [...]
>>>> {
>>>>     struct node base;
>>>>     struct node *sp;
>>>>     sp = &base;
>>>>     sp->number = 1;
>>>>     printf("%d\n", *sp);
>>>
>>> This is the same mistake you made before.
>>>
>>> "%d" requires an argument of type int.  What is the type of *sp?
>>> What do you expect this printf call to do?
>>
>>     IDK but
> [...]
>
> Then my advice is to stop and don't attempt anything else until you can
> answer those questions.

    The type is either struct node or struct node * and they're both very 
different. I tried casting both and it didn't work. The size of struct node 
is 16 bytes too I see.

Bill

[toc] | [prev] | [next] | [standalone]


#87504

From"Bill Cunningham" <nospam@nspam.invalid>
Date2016-05-09 14:51 -0400
Message-ID<ngqm19$39r$1@dont-email.me>
In reply to#87502
"Bill Cunningham" <nospam@nspam.invalid> wrote in message 
news:ngqlud$2vo$1@dont-email.me...
>
> "Keith Thompson" <kst-u@mib.org> wrote in message 
> news:lninynuldz.fsf@kst-u.example.com...
>> "Bill Cunningham" <nospam@nspam.invalid> writes:
>>> "Keith Thompson" <kst-u@mib.org> wrote in message
>>> news:ln60uoxmug.fsf@kst-u.example.com...
>>>> "Bill Cunningham" <nospam@nspam.invalid> writes:
>> [...]
>>>>> {
>>>>>     struct node base;
>>>>>     struct node *sp;
>>>>>     sp = &base;
>>>>>     sp->number = 1;
>>>>>     printf("%d\n", *sp);
>>>>
>>>> This is the same mistake you made before.
>>>>
>>>> "%d" requires an argument of type int.  What is the type of *sp?
>>>> What do you expect this printf call to do?
>>>
>>>     IDK but
>> [...]
>>
>> Then my advice is to stop and don't attempt anything else until you can
>> answer those questions.
>
>    The type is either struct node or struct node * and they're both very 
> different. I tried casting both and it didn't work. The size of struct 
> node is 16 bytes too I see.
>
> Bill

    I would say I do not know the proper specifier here. To printf. But the 
seg fault is at sp->next->number=2

Bill

[toc] | [prev] | [next] | [standalone]


#87516

FromKeith Thompson <kst-u@mib.org>
Date2016-05-09 13:51 -0700
Message-ID<lnzirzt0o4.fsf@kst-u.example.com>
In reply to#87504
"Bill Cunningham" <nospam@nspam.invalid> writes:
> "Bill Cunningham" <nospam@nspam.invalid> wrote in message 
> news:ngqlud$2vo$1@dont-email.me...
>>
>> "Keith Thompson" <kst-u@mib.org> wrote in message 
>> news:lninynuldz.fsf@kst-u.example.com...
>>> "Bill Cunningham" <nospam@nspam.invalid> writes:
>>>> "Keith Thompson" <kst-u@mib.org> wrote in message
>>>> news:ln60uoxmug.fsf@kst-u.example.com...
>>>>> "Bill Cunningham" <nospam@nspam.invalid> writes:
>>> [...]
>>>>>> {
>>>>>>     struct node base;
>>>>>>     struct node *sp;
>>>>>>     sp = &base;
>>>>>>     sp->number = 1;
>>>>>>     printf("%d\n", *sp);
>>>>>
>>>>> This is the same mistake you made before.
>>>>>
>>>>> "%d" requires an argument of type int.  What is the type of *sp?
>>>>> What do you expect this printf call to do?
>>>>
>>>>     IDK but
>>> [...]
>>>
>>> Then my advice is to stop and don't attempt anything else until you can
>>> answer those questions.
>>
>>    The type is either struct node or struct node * and they're both very 
>> different. I tried casting both and it didn't work. The size of struct 
>> node is 16 bytes too I see.
>
>     I would say I do not know the proper specifier here. To printf.

First decide what you want to print.  Then, and only then, think about
how to print it (which includes deciding what specifier to use).

>                                                                     But
> the seg fault is at sp->next->number=2

The seg fault occurs *after* the code we're talking about, the code that
you still don't understand.  I advised you to stop and figure out the
printf call before going on.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


Page 2 of 4 — ← Prev page 1 [2] 3 4  Next page →

Back to top | Article view | comp.lang.c


csiph-web