Thread: list head

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

    Question list head

    In C++, this would normally give me my first node with head and curr pointing to it and next would be '\0'.

    Data *head, *curr;
    head=new Data;
    curr=head ;
    curr->next=NULL;

    Now to implement this in C, would I need something like this :

    struct Data *head, *curr;
    head=(Data*)malloc(sizeof (Data)); //is this the same a new??
    curr=head;
    curr->next=NULL;

    and then to create another node, would I need to do
    curr->next=malloc(sizeof *Data);

    One last thing...can I increase or decrease the size of an array within the struct (according to user input) e.g

    char name[20] to char name [30]

    cheers
    sophie
    simple is always an understatement.....

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >pointing to it and next would be '\0'
    [pedantic]
    '\0' is a character with all bits zero, called a NUL. When a pointer points to nowhere it is called NULL. The two are not interchangeable.
    [/pedantic]

    >head=(Data*)malloc(sizeof (Data)); //is this the same a new??
    Yes, but in C you can write it better and it's always a good idea to check that malloc did not return NULL.
    if ( ( head = malloc ( sizeof *head ) ) != NULL )

    >can I increase or decrease the size of an array within the struct (according to user input)
    Yes, but you have to use dynamic arrays by declaring them as char *, allocating the initial size and then resizing with realloc.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    94
    I've just gone through the manual about realloc and it makes no sense to...I have no idea as to how I should be implementing it.

    Ohh is this what you meant by initializing it as char * e.g

    struct Node
    {
    char *name[20];
    int * id [5];
    struct Node *next;
    };
    simple is always an understatement.....

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Ohh is this what you meant by initializing it as char *
    Not quite:
    Code:
    /* pseudocode */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct test
    {
      char *name;
    };
    
    static void resize ( char * a )
    {
      char *temp;
      if ( ( temp = realloc ( a, 13 * sizeof *a ) ) != NULL )
        a = temp;
    }
    
    
    int main ( void )
    {
      struct test a;
      if ( ( a.name = malloc ( 6 * sizeof a.name ) ) != NULL ) {
        strcpy ( a.name, "Fooby" );
        puts ( a.name );
        resize ( a.name );
        strcpy ( a.name, "Fooby Rules!" );
        puts ( a.name );
        free ( a.name );
      }
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > head=(Data*)malloc(sizeof (Data)); //is this the same a new??
    Depends.

    If Data is just a struct, then they are the same (except in the behaviour when you're out of memory - malloc returns NULL, new should throw an exception)

    If Data is a class, then they are different, because new will call the class constructor (assuming you had defined one)

    And C++ has no equivalent of realloc, though you can get a work-alike by calling new and delete yourself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  2. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  3. Linked List Help
    By Perverse in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2005, 08:33 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM