help on Linked list

This is a discussion on help on Linked list within the C Programming forums, part of the General Programming Boards category; hi, i know we can vary the no. of records in the list but can we vary the record size ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    11

    help on Linked list

    hi,

    i know we can vary the no. of records in the list but can we vary the record size itself.

    eg

    Code:
    typedef struct {
    
    int data;
    char *name;
    struct Recrd *next;
    }Recrd;
    
    Recrd *head=NULL, *cur=NULL;
    is it possible to vary the length of name [structure variable]

    i prefer to give the length of string variable at run time than at compile time[name[30]].

    any help, will be appreciated.
    Last edited by supi; 05-22-2008 at 01:19 AM. Reason: missed out a line in code

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes?

    Remember it's just a char pointer, when you create a new node/record allocate memory for it (how ever much you need)

    ie,
    Code:
    Recrd rc;
    
    rc.name = malloc(5);
    strcpy(rc.name, "Hello");       /* just to demonstrate ;) */
    And when you remove/delete the record don't forget to free rc.name.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Code:
    rc.name = malloc(5);
    strcpy(rc.name, "Hello");
    you really need at least one char more to store the nul-character.
    In the current code you have a memory overrun
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Oops, I knew that I just couldn't count that Hello = 5, not 4.

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    11
    thanks

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    Quote Originally Posted by supi View Post
    hi,

    i know we can vary the no. of records in the list but can we vary the record size itself.

    eg

    Code:
    typedef struct {
    
    int data;
    char *name;
    struct Recrd *next;
    }Recrd;
    
    Recrd *head=NULL, *cur=NULL;
    is it possible to vary the length of name [structure variable]

    i prefer to give the length of string variable at run time than at compile time[name[30]].

    any help, will be appreciated.

    if the data is an array means you cant give the size of the array at the run time....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

Tags for this Thread


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