?
Solved

dynamic string

Posted on 2006-05-13
2
Medium Priority
?
254 Views
Last Modified: 2010-04-15
I have a  code like this

typedef struct node {
    char str2[50];       // i dont want to allocate fixed array for my string
    struct node *next;
}NODE;


since I dont like to have the fixed array in my struct I have tried to create a dynmics string fn

char *new_string (char *src, int len)
{
    char *alloc;
    alloc = malloc (len+1);
    strncpy (alloc, src, 2);
    alloc[2] = 0;
    return (alloc);
}

but this is not a good solution as one must know the length of the string in before hand, and must remember to deallocate/free memory for the function.
The string is given through command line and the user shouldn't bother about how long the string is etc.

so the question is how do we create a dynamic string?
0
Comment
Question by:Thunder_scream
2 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16676427
Hi Thunder_scream,

> but this is not a good solution as one must know the length of the
> string in before hand,
No you dont have to know the string length ... neither malloc nor strncpy require you to know the length ...
strncpy (alloc, src, len);
alloc[len]=0;
would serve you just as well ...

Check return value from malloc, it can return NULL ...
alloc is also a function to allocate memory ... suggested to use a different name for the variable to avoid confusions

>and must remember to deallocate/free memory for
> the function.
this you must ... but that would be part of code that deletes node or entire list ... should not be a problem

> The string is given through command line and the user shouldn't bother
> about how long the string is etc.
User still does not have to know the length or bother about it
 
> so the question is how do we create a dynamic string?
Does the above satisfy your requirements or is there something else too ?

Cheers!
Sunnycoder
0
 
LVL 24

Accepted Solution

by:
fridom earned 375 total points
ID: 16676598
You are in the end looking for something like strdup (however it's a better idea not to use this name in you normal C code)

or dupstr, for Unices this might be near standard but it's not very hard to do it yourself.

the code is similiar to this:

char * dup_str (char *str){
   size_t len = strlen(str); // assuming str is valid
   char *result = malloc(len+1); // assuming this will never faile ;-(
   strcpy(result, str);
   return result;
}

You do not need any extra length parameter.

Regards
Friedrich

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question