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


Groups > comp.lang.c > #22049

Re: linked list

From Ike Naar <ike@sverige.freeshell.org>
Newsgroups comp.lang.c
Subject Re: linked list
Date 2012-06-09 06:45 +0000
Organization A noiseless patient Spider
Message-ID <slrn3vfsjt5s81.l5k.ike@sverige.freeshell.org> (permalink)
References <bc9a4156-754e-4251-bfab-d75241dad420@googlegroups.com> <slrn3vfsjt5o3c.fhi.ike@sverige.freeshell.org> <716df42e-ee26-4419-8adf-f17a9f18125f@googlegroups.com>

Show all headers | View raw


On 2012-06-09, Zhang Yuan <zhabgyuan1993@gmail.com> wrote:
> On Saturday, June 9, 2012 1:34:36 PM UTC+8, Ike Naar wrote:
>> On 2012-06-09, Zhang Yuan <zhabgyuan1993@gmail.com> wrote:
>> > struct stu{
>> >     int num;
>> >     char name[12];
>> >     int age[0];
>> 
>> There is room for zero ages in the struct ...
>> 
>> >     struct stu *next;
>> >     };
>>
>> >         scanf("%s%d",curr->name,curr->age);
>> 
>> ... but here you try to store something in curr->age.
>> Since curr->age has size zero, the address of curr->age
>> can be the same as the address of curr->next, and storing
>> a value in curr->age may overwrite whatever was in curr->next.
>
> Why the address of curr->age can be the same as the address of curr->next,
> not the address of curr-name[12],or other?

6.7.2.1 p13 of the standard (Structure and union specifiers) says:
"Within a structure object, the non-bitfield members and the units in
which bitfields reside have addresses that increase in the order in
which they are declared."

That means, for struct stu S,

  (char*)&S.num <= (char*)&S.name <= (char*)&S.age <= (char*)&S.next

Since sizeof S.num and sizeof S.name are nonzero, it follows that

  (char*)&S.num < (char*)&S.name < (char*)&S.age <= (char*)&S.next

so the only members that can have the same address are S.age and S.next .

There can be padding between S.age and S.next, so it is
not *required* that they have the same address.

Just out of curiousity: why did you declare age as a zero-sized array?

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


Thread

linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-08 21:02 -0700
  Re: linked list Kaz Kylheku <kaz@kylheku.com> - 2012-06-09 04:59 +0000
    Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-08 22:13 -0700
    Re: linked list Ian Collins <ian-news@hotmail.com> - 2012-06-09 17:47 +1200
      Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-08 22:59 -0700
        Re: linked list Ian Collins <ian-news@hotmail.com> - 2012-06-09 18:35 +1200
          Re: linked list Ike Naar <ike@sverige.freeshell.org> - 2012-06-09 06:53 +0000
            Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-09 00:53 -0700
    Re: linked list 88888 Dihedral <dihedral88888@googlemail.com> - 2012-06-22 12:18 -0700
  Re: linked list Ike Naar <ike@sverige.freeshell.org> - 2012-06-09 05:34 +0000
    Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-08 22:51 -0700
      Re: linked list Ike Naar <ike@sverige.freeshell.org> - 2012-06-09 06:45 +0000
        Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-08 23:56 -0700
    Re: linked list Keith Thompson <kst-u@mib.org> - 2012-06-09 11:17 -0700
      Re: linked list Ike Naar <ike@iceland.freeshell.org> - 2012-06-09 22:02 +0000
  Re: linked list Ian Collins <ian-news@hotmail.com> - 2012-06-09 17:49 +1200
    Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-08 22:53 -0700
      Re: linked list Ian Collins <ian-news@hotmail.com> - 2012-06-09 18:36 +1200
      Re: linked list Keith Thompson <kst-u@mib.org> - 2012-06-09 11:20 -0700
  Re: linked list pete <pfiland@mindspring.com> - 2012-06-09 06:49 -0400
    Re: linked list pete <pfiland@mindspring.com> - 2012-06-09 07:28 -0400
      Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-09 07:57 -0700
        Re: linked list Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-09 19:16 +0100
        Re: linked list pete <pfiland@mindspring.com> - 2012-06-09 16:01 -0400
        Re: linked list pete <pfiland@mindspring.com> - 2012-06-09 16:50 -0400
          Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-09 18:21 -0700
            Re: linked list pete <pfiland@mindspring.com> - 2012-06-10 00:11 -0400
              Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-09 22:20 -0700
                Re: linked list pete <pfiland@mindspring.com> - 2012-06-10 08:31 -0400
            Re: linked list nick_keighley_nospam@hotmail.com - 2012-06-11 06:10 -0700
              Re: linked list Zhang Yuan <zhabgyuan1993@gmail.com> - 2012-06-11 09:16 -0700
              Re: linked list Keith Thompson <kst-u@mib.org> - 2012-06-11 15:45 -0700
                Re: linked list Michael Press <rubrum@pacbell.net> - 2012-06-14 12:47 -0700

csiph-web