dynamically allocated strings??

This is a discussion on dynamically allocated strings?? within the C Programming forums, part of the General Programming Boards category; Ok, part of my assignment for school is to create a struct with 2 char pointers in it. These pointers ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    dynamically allocated strings??

    Ok, part of my assignment for school is to create a struct with 2 char pointers in it. These pointers are supposed to point to a couple of dynamically allocated strings and I'm supposed to create a little function that prints the contents of the struct out. So far I have the struct:
    Code:
    typedef struct
    {
    	char *name;
    	char *number;
    }myStruct;
    And I have the print function:
    Code:
    void printMyStruct(myStruct *a, myStruct *b)
    {
         printf("%s: %s\n", *(a).name, *(b).number);
    }
    I think those two parts are right. My question is about the dynamically allocated strings that the pointers in my struct are supposed to point to. I'm not quite sure of how to do that..

  2. #2
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Use the malloc() function.

    For example:

    Code:
    char *string = malloc(sizeof(char) * 100);
    . . . for a string of a size of 100 characters.

    EDIT: don't forget to free the memory when you're done with it . . .
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    second part is wrong . has precedence over the * so it should not even compile

    should be

    printf("%s: %s\n", a->name, b->number);

    to allocate string use malloc

    Code:
    a->name = malloc(strlen(temp)+1);
    if(a->name)
        strcpy(a->name,temp);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    I thought that's what it was but I wasn't sure because the instructions say that the strings shouldn't have any wasted space.. So, is it ok to do something like this?

    Code:
    int capacity = 1;
    int size = 0;
    int c;
    char *string = malloc(sizeof(char) * capacity);
    while((c = fegetc(stdin) != EOF)
    {
         string[size++] = c;
         if(size == capacity)
         {
              string = realloc ( string ,capacity * sizeof (char));
         }

  5. #5
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Yes, it is, but be careful, because realloc() and malloc() can fail . . .
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Right. I didn't include any code to check if they return NULL. I know I should for my program though. In the code I just posted I used fgetc(). Would it be possible to substitute fgets() instead?

  7. #7
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Hmm. Yes, it is possible, but it would be quite useless, as you'd be using it in the same position as fgetc() unless you read into a huge temporary buffer.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Cool. Thank you. I'll post again if I run into any problems

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Code:
    char *string = malloc(sizeof(char) * 100);
    . . . for a string of a size of 100 characters.
    You meant 99?

  10. #10
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Yes, yes, I'm sorry. My brain's on holidays tonight.

    A string of 100 including the NULL, is what I meant . . .
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Ok. One quick, little question. How do I get one of the pointers in my struct to point to the string??

  12. #12
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Like so . . .
    Code:
    a->name = localstring;
    . . . for example. localstring is the malloc'd char *.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Hmm.. that's how I was trying to do it but I keep getting this error when I try to compile:

    error: invalid type argument of ->

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by CS_Student8337 View Post
    I thought that's what it was but I wasn't sure because the instructions say that the strings shouldn't have any wasted space..
    in this case - use temporary static buffer to enter string and after that use strlen to detrmine the needed space to be allocated like in my example
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by CS_Student8337 View Post
    Hmm.. that's how I was trying to do it but I keep getting this error when I try to compile:

    error: invalid type argument of ‘->’
    is a pointer or struct?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamically allocated size
    By maverickbu in forum C++ Programming
    Replies: 12
    Last Post: 06-26-2007, 01:16 PM
  2. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  3. scope of dynamically allocated variables
    By lanzyzhang in forum C Programming
    Replies: 4
    Last Post: 07-20-2004, 10:59 AM
  4. I need a dynamically allocated linked list
    By Flex in forum C Programming
    Replies: 2
    Last Post: 03-06-2002, 01:28 PM

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