Thread: Whats Wrong in My simple program?

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    4

    Whats Wrong in My simple program?

    Code:
    #include <malloc.h>
    #include <stdio.h>
    struct list
    {
    int data;
    struct list next;
    
    
    
    };
    typedef struct list node;
    typedef node link;
    main()
    {
    link ptr,head;
    int num,i;
    ptr=(link)malloc(sizeof(node));
    ptr=head;
    printf("please input 5 numbers==>\n");
    for(i=0;i<=4;i++)
    {
       scanf("%d",&num);
       ptr->data=num;
       ptr-next=(link)malloc(sizeof(node));
       if(i==4) ptr->next=NULL;
       else  ptr=ptr->next;
    
    
    
    }
    ptr=head;
    while(ptr!=NULL)
    {
      printf("The value is ==>%d\n",ptr->data);
      ptr=ptr->next;
    }
    }
    what's wrong with it? help me, thanx..
    change the first error,thanx,may be i should rewrite this program,i am newbit , sorry at all...
    Last edited by MartinLiao; 09-02-2004 at 02:36 AM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Your #include's are incorrect. Aside from missing the '#' for the first one entirely (probably a copy & paste error) you should use <> around the header name instead of "".

    You think you're using pointers, but you're not. Your 'ptr' variable is actually an instance of the struct; not a pointer to one.

    Your spacing and indenting is atrocious. Cleaning it up might reveal some errors.

    Get those things fixed and let us know if you're still having problems and what the problems actually are. What symptoms are you seeing? Try to be a little more specific than "what's wrong with it?" with no explanation of what's happening as opposed to what you expect to happen.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  3. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  4. Help with small simple program
    By Sonor in forum C Programming
    Replies: 5
    Last Post: 04-02-2005, 07:40 AM
  5. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM