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


Groups > comp.lang.c > #87442

Re: trying a linked list

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: trying a linked list
Date 2016-05-08 14:56 -0700
Organization None to speak of
Message-ID <lnlh3kw6w4.fsf@kst-u.example.com> (permalink)
References (8 earlier) <ngo0s4$g9h$1@dont-email.me> <87d1owgyev.fsf@bsb.me.uk> <ngo3q2$r9a$1@dont-email.me> <87twi8fikb.fsf@bsb.me.uk> <ngoa7k$jg5$1@dont-email.me>

Show all headers | View raw


"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"

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web