link list question

This is a discussion on link list question within the C Programming forums, part of the General Programming Boards category; If i had the follwoing link list structure Code: struct list { int data; char *moredata; struct list next; } ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    19

    link list question

    If i had the follwoing link list structure

    Code:
    struct list
    {
       int data;
       char *moredata;
       struct list next;
    }
    
    typedef struct list LIST;
    How would i be using the data and moredata variables? What i mean to say is how do i actually referecnce or acknowledge them when i want to use them in my program?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    19
    sorry it should look like this

    Code:
    struct list
    {
       int data;
       char moredata;
       struct list next;
    }
    
    typedef struct list LIST;

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct list
    {
       int data;
       char *moredata;
       struct list next;
    }
    *ponder* I wonder if by declaring a structure that contains itself, if you can overflow your stack by creating an instance of that variable. Wouldn't that be fun!

    You need a pointer rather than an actual instance of the structure.

    Change this line:

    struct list next;

    To this:

    struct list *next;

    To answer your question, you use them the same way you would normally use variables in a structure. If you're not using a pointer to it, you use the dot operator, otherwise you use the arrow operator.

    Vola!

    struct list MyList;
    struct list *MyPtr;

    MyList.data = x;
    MyPtr = &MyList;

    MyPtr->data = x;



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

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by alokin
    sorry it should look like this

    Code:
    struct list
    {
       int data;
       char moredata;
       struct list next;
    }
    
    typedef struct list LIST;
    No, it actually should look like this:

    Code:
    struct list
    {
       int data;
       char moredata;
       struct list *next;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

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. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 11:30 AM
  3. List Question
    By ConsulVortex in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2006, 03:38 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. Link List ???
    By justin69enoch in forum C Programming
    Replies: 3
    Last Post: 11-23-2002, 10:34 PM

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