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


Groups > comp.lang.c > #22047

Re: linked list

Path csiph.com!usenet.pasdenom.info!news.albasani.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: linked list
Date Sat, 09 Jun 2012 18:35:24 +1200
Lines 50
Message-ID <a3g95cFr2hU6@mid.individual.net> (permalink)
References <bc9a4156-754e-4251-bfab-d75241dad420@googlegroups.com> <20120608213614.855@kylheku.com> <a3g6c9Fr2hU4@mid.individual.net> <acfbc6f4-9582-4692-bab3-eead1dd0a3ce@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net lj+7agBliycAW8NyUIU+8AWpdmnbxq5Lw0Oj26uHeuGARcQ/MzQT76w+dUD3kYEXTg
Cancel-Lock sha1:gTRDujGFkLGN6pOGLkUjykB6d90=
User-Agent Mozilla/5.0 (X11; SunOS i86pc; rv:12.0) Gecko/20120423 Thunderbird/12.0
In-Reply-To <acfbc6f4-9582-4692-bab3-eead1dd0a3ce@googlegroups.com>
Xref csiph.com comp.lang.c:22047

Show key headers only | View raw


On 06/ 9/12 05:59 PM, Zhang Yuan wrote:
> On Saturday, June 9, 2012 1:47:53 PM UTC+8, Ian Collins wrote:
>> On 06/ 9/12 04:59 PM, Kaz Kylheku wrote:
>>> On 2012-06-09, Zhang Yuan<zhabgyuan1993@gmail.com>   wrote:
>>>> ----------------------------------------------------------------------------
>>>> #include<stdio.h>
>>>> #include<stdlib.h>
>>>> struct stu{
>>>>       int num;
>>>>       char name[12];
>>>>       int age[0];
>>>>       struct stu *next;
>>>>       };
>>>>
>>>> int main(){
>>>>
>>>> struct stu *curr,*head;
>>>>
>>>> head=(stu *)malloc(sizeof(stu));
>>>
>>> If you're really working in C and not C++, you want sizeof (struct stu)
>>> here (just like in your other malloc call below).
>>>
>>> (In C++, a struct stu { ... } declaration will introduce stu as a type name,
>>> but not in C.
>>>
>>> In C, if you have some defined object called "stu" somewhere, or a type "stu",
>>> this sizeof will measure the size of that object or type, and not "struct stu".
>>>
>>> Example:
>>>
>>>     int stu;
>>>
>>>     head = (stu *) malloc(sizeof(stu)); /* actually sizeof int */
>>>     head = (stu *) malloc(sizeof(struct stu)); /* size of the structure */
>>
>> Which one reason why
>>
>>     head = malloc( sizeof *head );
>>
>> is a good idea!
>
> Sorry,I don't understand very well.
> sizeof *head ==4 0r ==sizeof struct?

"sizeof *head" translates to "size of the type of head".  So whatever 
type head is, malloc will return the correct size block.

-- 
Ian Collins

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