Menu

what is wrong?

Jim Morris
2008-02-13
2012-09-26
  • Jim Morris

    Jim Morris - 2008-02-13

    Could you please help me?
    what is wrong with this code?
    it prints only D->D->D->D (I want A->B->C->D)

    struct node / Declaration of structure "node" /
    {
    char letter[1];
    struct node *next ;
    };

    struct node *start = NULL;

    main(void)
    {
    struct node current = start;
    struct node
    new_ptr;

    new_ptr = (struct node *)malloc(sizeof(struct node));
    new_ptr->next = NULL;
    strcpy(new_ptr->letter, "A");
    start = new_ptr;
    current = new_ptr;

    new_ptr = (struct node *)malloc(sizeof(struct node));
    new_ptr->next = NULL;
    strcpy(new_ptr->letter, "B");
    current->next = new_ptr;
    current = new_ptr;

    new_ptr = (struct node *)malloc(sizeof(struct node));
    new_ptr->next = NULL;
    strcpy(new_ptr->letter, "C");
    current->next = new_ptr;
    current = new_ptr;

    new_ptr = (struct node *)malloc(sizeof(struct node));
    new_ptr->next = NULL;
    strcpy(new_ptr->letter, "D");
    current->next = new_ptr;
    current = new_ptr;

    current = start;
    printf("%s->\n ",new_ptr->letter);
    while(current != NULL)
    {
    printf("%s-> ",new_ptr->letter);
    current = current->next;
    }
    system("PAUSE");
    }

     
    • BiT

      BiT - 2008-02-13

      your problem is in your while loop

      /***/
      what is my pointer pointing to
      does that ever change
      is my pointer ever pointing to anything else

      you get the drift...I would just tell you but this may be homework
      /***/

      while(current != NULL)
      {
      printf("%s-> ",new_ptr->letter);
      current = current->next;
      }

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.