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


Groups > comp.lang.c > #22058

Re: linked list

Message-ID <4FD33350.63FA@mindspring.com> (permalink)
Date 2012-06-09 07:28 -0400
From pete <pfiland@mindspring.com>
Organization PF
Newsgroups comp.lang.c
Subject Re: linked list
References <bc9a4156-754e-4251-bfab-d75241dad420@googlegroups.com> <4FD32A2F.1A9E@mindspring.com>

Show all headers | View raw


pete wrote:
> 
> Zhang Yuan 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));
> > curr=head;
> > scanf("%d",&(head->num));
> > curr->next=NULL;
> >
> > while(curr->num!=0){
> >         scanf("%s%d",curr->name,curr->age);
> >         curr=(stu *)malloc(sizeof(struct stu));
> >         scanf("%d",&(curr->num));
> >         curr->next=NULL;
> > }
> > return 0;
> > }
> >
> > --------------------------------------------------------------------------
> > it  doesn't work.When I debug:
> >
> > input :
> > 101 zhang 19
> >
> > the memory information:4 pictures
> > http://flic.kr/p/ccMQey
> >
> > why 0x6f17e0 change from 00-->13 unexpectedly.
> > I think the "*next" and "int age"storaged in different place.
> > but it looks like it overwrite?
> 
> #include <stdio.h>
> #include <stdlib.h>
> 
> struct stu{
>     struct stu *next;
>     int num;
>     char name[12];
>     int age;
>     };
> 
> int
> main(void)
> {
>     struct stu *curr,*head;
> 
>     head = malloc(sizeof *head);
>     if (head != NULL) {
>         curr = head;
>         puts("enter number");
>         scanf("%d", &(head -> num));
>         curr -> next = NULL;
>         while(curr -> num != 0){
>             puts("enter name and age");
>             scanf("%s%d", curr -> name, &(curr -> age));
>             curr = malloc(sizeof *curr);

That doesn't make any sense!

/* BEGIN new.c */

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

struct stu{
    struct stu *next;
    int num;
    char name[12];
    int age;
    };

void stu_free(struct stu *head);
void stu_print(struct stu *head);

int 
main(void)
{
    struct stu *curr,*head;
    
    puts("/* BEGIN new.c */ output\n");
    head = malloc(sizeof *head);
    if (head != NULL) {
        curr = head;
        curr -> next = NULL;
        puts("enter number");
        if (1 > scanf("%d", &(head -> num))) {
            puts("\n\nscanf problem.\n\n");
            exit(EXIT_FAILURE);
        }
        while(curr -> num != 0) {
            puts("enter name and age");
            scanf("%s%d", curr -> name, &(curr -> age));
            if (1 > scanf("%d", &(head -> num))) {
                puts("\n\nscanf problem.\n\n");
                exit(EXIT_FAILURE);
            }
            curr -> next= malloc(sizeof *(curr -> next));
            if (curr -> next == NULL) {
                break;
            }
            curr = curr -> next;
            curr -> next = NULL;
            puts("enter number");
            scanf("%d", &(curr -> num));
        }
        *(curr -> name) = '\0';
        curr -> age = 0;
    }
    stu_print(head);
    stu_free(head);
    puts("\n/* END new.c */ output");
    return 0;
}


void 
stu_free(struct stu *head)
{
    struct stu *next;

    while (head != NULL) {
        next = head -> next;
        free(head);
        head = next;
    }
}

void 
stu_print(struct stu *head)
{
    while (head != NULL) {
        printf("\nnumber == %d\n", head -> num);
        printf("name is %s\n", head -> name);
        printf("age is %d\n", head -> age);
        head = head -> next;
    }
}


/* END new.c */

-- 
pete

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