allocate memory for constant string?

This is a discussion on allocate memory for constant string? within the C Programming forums, part of the General Programming Boards category; I have a struct Code: struct animation { char *name; } I am entering the animation names manually, e.g. "standstill" ...

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

    allocate memory for constant string?

    I have a struct

    Code:
    struct animation 
    {
     char *name;
    }
    I am entering the animation names manually, e.g. "standstill"

    I want to allocate memory depending on the size of the constant string so my function is:
    Code:
    char* dynamic_str_pointer(const char *my_string)
    {
    return (char *)malloc(sizeof(char) * (strlen(my_string)+1));
    }
    I am calling it like this:

    Code:
    animation a;
    a.name=dynamic_str_pointer("standstill");
    but the compiler doesnt like it, it just says there are multiple definitions of the function (but there are not)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Post your exact error messages.

    Though my guess is that you've put the code in a header file, and included that header file in several source files.

    I hope your dynamic_str_pointer has a NULL check (for malloc) and a strcpy as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    oh yes I did include it in multiple source files
    after fixing it, I cant seem to print the string

    Code:
    printf("anim name %s\n",a.name);
    it just doesnt print anything after anim name
    Last edited by slenkar; 09-17-2012 at 09:50 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Like I said, did you also COPY the string, as well as allocating space for it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    ohh ok thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2012, 04:28 AM
  2. pre-allocate memory for string
    By TheBigOnion in forum C++ Programming
    Replies: 6
    Last Post: 03-30-2010, 01:10 PM
  3. Cannot allocate an array of constant size 0
    By steve1_rm in forum C Programming
    Replies: 9
    Last Post: 05-26-2009, 03:57 AM
  4. Allocate memory inside allocated memory block?
    By Heidi_Nayak in forum C Programming
    Replies: 14
    Last Post: 04-15-2009, 04:19 PM
  5. Replies: 28
    Last Post: 12-10-2004, 05:49 AM

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