Problem making strings in a malloc-ed struct

This is a discussion on Problem making strings in a malloc-ed struct within the C Programming forums, part of the General Programming Boards category; I have a struct with the following definition: Code: struct node{ char * name; struct node * next; }; The ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    12

    Problem making strings in a malloc-ed struct

    I have a struct with the following definition:

    Code:
    struct node{
       char * name;
       struct node * next;
    };
    The problem is that I want to make a linked list of these nodes, each having its own name. These names are to come from standard input -- that is, they are not static. I can't figure out how to put a string into the "name" field of a dynamically allocated node so that it doesn't disintegrate when the function that it was created within completes execution. I'm pretty sure there's no way to malloc a string. Does anyone have any suggestions? I've been working on this one little problem for over 40 minutes.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Besides making name an array and make sure it's not overflown at input, you can also allocate memory for it separately.

    Assuming the string, 'string' exists.
    Code:
    struct node *node = malloc(sizeof(struct node));
    node->name = malloc(strlen(string)+1);

    Edit: strdup() is a much better suited function to use here. Still, you could use this two step approach if you are creating nodes dynamically.
    Last edited by Subsonics; 03-28-2011 at 08:02 PM. Reason: strdup()

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,559
    You have to read it into a temporary buffer, then allocate space and copy:
    Code:
    char buf[100];
    struct node n;
    
    fgets(buf, sizeof(buf), stdin);
    // clean up input, remove any new lines fgets leaves
    n.name = strdup(buf);
    This is a very simple example. It may run into problems if the user wants to enter more than 100 chars. You can make the buffer larger, or do a loop of fgets reads with malloc/realloc and strcpy if you need lots more space.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Take note that strdup() is non standard function.
    Even though it's not hard to implement yourself.

    Edit: strdup is in C99.
    Last edited by Bayint Naung; 03-28-2011 at 08:25 PM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    Thanks! I had no idea about strdup. For some reason I just assumed malloc-ing strings was a faux pas, but I couldn't see any other way around this problem. cheers

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,559
    Quote Originally Posted by itsthemac View Post
    Thanks! I had no idea about strdup. For some reason I just assumed malloc-ing strings was a faux pas, but I couldn't see any other way around this problem. cheers
    I should have mentioned that strdup is not part of the standard. It's a proposed add-on to the C99 standard, thus you might not find it on all platforms/compilers. As a workaround, you can do what Subsonics suggested (with the obvious strcpy afterward).

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by itsthemac View Post
    Thanks! I had no idea about strdup. For some reason I just assumed malloc-ing strings was a faux pas, but I couldn't see any other way around this problem. cheers
    Well, think about it... what is a string?
    It is an array of characters. The only thing that makes it any different from any other array is the trailing 0 at the end.

    Your first option is to use the array style declaration in your struct...
    Code:
    struct t_LList
      {  char text[100];
         t_LList* next; }
    Also malloc() then strcpy() or strdup() (really just strcpy with malloc included) are perfectly legitimate ways of initializing a char* array as others have already shown you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with malloc(), maybe extern??
    By Dimes in forum C Programming
    Replies: 10
    Last Post: 12-08-2010, 06:53 AM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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