Thread: linked list inside a structure

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    linked list inside a structure

    I have a structure which I made into a binary search tree. It reads each word from a file and inserts into tree.

    Now I am trying to implement a linked list to hold the offsets for a word each time it is in the file.

    I am having trouble adding a linked list inside a structure and cannot seem to find any references. Could someone point me in the right direction? Here is my attmept:

    Code:
    struct test{
    long off;
    struct test *next;
    };
    
    //basic structure to hold abstract search words
    struct words{
    	char word[51];	//data word
    	int count;	//counter for reoccurences
    	struct words * left;	//left child node
    	struct words * right;	//right child node
    	struct test * temp;
    };
    typedef struct words * RECPTR;
    
    
    RECPTR->temp->off = ftell(fp);
    would something like this not work?

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    Hiding pointers with typedef? Do you have a reason to do this, other than just to hide the pointer? In a situation where you're making a complex set of nested function calls which accept other function addresses as arguments it might be useful and actually increase readability. But being careless with this convenience might result in some frustrating to debug/manage applications if your project gets big enough... if you're determined at least use a 'special' prefix or suffix as a visual indicator to express the fact that type is actually a pointer.

    edit: Never mind, it just hit me; RECPTR. I'll just keep this comment here as FYI for random readers, sorry I contributed nothing.

    What you want to do should be possible as I understand it, what problems are you actually having? You'll get better responses if you actually attempt the exercise and post back on the exact issues you're experiencing in implemented the program. If you asked me if anything is possible, I would say yes, but that doesn't help you. Instead go as far as you can until you hit a wall, then let us know what wall you ran in to, from there we can point you back on to the path. :]
    Last edited by since; 12-14-2009 at 02:22 PM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It should work fine, assuming you actually have something allocated for test. Oh, and I too hate typedef-ing pointers.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    Ok well obviously I have a lot more code then listed here, but everything was working until I tried adding the linked list.

    I am getting a seg fault. How would I allocate memory for a structure inside a structure?

    Here is how I am currently trying to allocate the memory for the struct test:
    Code:
    RECPTR makeRoom ( void )
    {
      RECPTR new;     /* a local pointer */
            //dynamic allocation of memory and saving the location to new
      new->temp = ( struct test * ) malloc ( sizeof ( struct test ) );
      return new;    /* send address of new node back */
    
    }
    But this is not seeming to work. I think i am referencing it wrong or something. For each structure of words there is a going to be a unique linked list. so if there is 50 words added there would be 50 unique linked lists . .. does that make sense?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to allocate two blocks of memory. One for the outside structure. One for the inner structure.
    Code:
    struct foo
    {
        struct bar *baz;
    };
    ...
    
    struct foo *p;
    
    p = malloc( sizeof( struct foo ) );
    ...
    
    p->baz = malloc( sizeof( struct bar ) );
    I assumed you were using previously allocated memory. But anyway, what you want is something like the above.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    ok cool thanks man. Trying it now...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM