Linked List problem :(

This is a discussion on Linked List problem :( within the C Programming forums, part of the General Programming Boards category; Ok, here is the part that doesnt work Obviously, i must be mistunderstanding something, so if you could point me ...

  1. #1
    fry
    fry is offline
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128

    Unhappy Linked List problem :(

    Ok, here is the part that doesnt work

    Obviously, i must be mistunderstanding something, so if you could point me in the right direction, that would be great

    Code:
    IORB list;
    IORB *tail = &list;
    .
    .
    IORB *temp = tail;
    printf("%hd \n", tail->base_pri);
    printf("%hd \n", temp->base_pri);
    tail is declared as a pointer to an IORB list.
    When running this code, the two lines produce different results. They (appear) to be (supposedly) pointing to the same thing, but obviously are not

    Thanks
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    They should point to the same thing. Here's some sample code (note the format modifiers have changed
    Code:
    #include <stdio.h>
     
     typedef struct IORB
     {
       char *base_pri;
     } IORB;
     
     int main(void)
     {
       IORB list;
       IORB *tail = &list;
       IORB *temp = tail;
       
       tail->base_pri = NULL;
       printf("%p \n", (void*)tail->base_pri);
       printf("%p \n", (void*)temp->base_pri);
       tail->base_pri++;
       printf("%p \n", (void*)tail->base_pri);
       printf("%p \n", (void*)temp->base_pri);
       
       return 0;
     }
     
     /* 
     Output
     
     00000000
     00000000
     00000001
     00000001
     
     */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    fry
    fry is offline
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    Thanks Hammer.

    That solved my problem somewhat... I have ended up starting my code from scratch again... I cant seem to add a node to the list. It was a problem in my code before as well, and i still cant figure it out.

    Code:
    #include <stdio.h>
     typedef struct iorb
     {
       short int base_pri;
       struct iorb *link;
     } IORB;
    
    
       IORB list;
       IORB *tail = &list;
       IORB *temp = tail;
    
    #include "list.h"
     
     int main()
     {
       int menu;
       short int temp_pri = 0;
     
       
       tail->base_pri = 0;
       
       add(temp_pri);
       temp = tail;
    
       display();
       
       scanf("%d", &menu);
       
       return 0;
     }
    
    
    
    //// list.h
    
    int add(short int pri){
       IORB *node;
       tail->link = node;
       tail = tail->link;
       tail->base_pri = pri;
       tail->link = NULL;
       return 0;
    }
    
    
    int display(){
       IORB *temp = &list;
       while(temp)
       {
           printf("%hd\n", temp->base_pri);
           temp = temp->link;
       }
    }
    Sorry for posting it all. Its also messy because i couldn't get the passing to work between functions...

    Anyway, when i run this code, it crashes. But if i take out the variable being passed to the add() function. ie:
    Code:
    int add()
    then it works fine. :S Logic seems to be right, so its a runtime error... any ideas?
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > IORB *node;
    In add() you create a new node, but never allocate any memory for it.

    node = malloc(sizeof( *node) );

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    2
    ya! you need to allocate memory for the structure, before moving values.
    the allocation may be node=(IORB *)malloc(sizeof(IORB *));
    if you leave it, the node remains tangling. & junk memory gets allocated.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,660
    >> sizeof(IORB *)
    This only allocates enough space for a pointer to IORB.
    You want sizeof(IORB) or as previously posted, sizeof(*node).

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21