Solved

How to implement stringlist ???

Posted on 2001-08-15
3
270 Views
Last Modified: 2010-04-15
Hi experts,

I want to implement a common stringlist.
My stringlist.h with my structire looks like:

typedef struct _stringlist {
        char    **strings;
        size_t    count;
} StringList;

Now i want to implement the following function:

StringList* stringlist_insert(StringList* list,
                              char* name)

My initializing function looks like:

StringList* stringlist_initialize()
{
    StringList *stringlist = malloc(sizeof(StringList));
    if (stringlist == NULL)
        return NULL;
   
        stringlist->count = 0;
        stringlist->strings = malloc(stringlist->sl_max*
                                     sizeof(char *));
        if (stringlist->strings == NULL)
            return NULL;
       
        return stringlist;
}


I have little problems with pointers, so can
anybody please help me , how to implement the
insert function ???????????????????????????????


Many thanks
katjas
0
Comment
Question by:katjas
  • 2
3 Comments
 
LVL 6

Expert Comment

by:kotan
ID: 6387279
is sl_max member of stringlist?
if then, what does it for?
is it number of string or maximum char allow in one string?
0
 
LVL 6

Accepted Solution

by:
kotan earned 100 total points
ID: 6387321
typedef struct _stringlist
{
     char    **strings;
        size_t    count;
     int sl_max;
} StringList;

Let's said sl_max is number of string allow in StringList.

In stringlist_initialize(), the stringlist->strings allocation should be

stringlist->strings = (char**)malloc(sizeof(char*[stringlist->sl_max]));

for stringlist_insert(), assume that the parameter name is the string to be added.

StringList* stringlist_insert(StringList* list, char* name)
{
     if (list->count >= list->sl_max)
     {
          // The list is full.
          return NULL;
     }

     list->strings[list->count] = (char*) malloc(strlen(name)*sizeof(char) + 1);
     strcpy(list->strings[list->count], name);
     (list->count)++;

     return list;
     
}

Then, if you want to free the memory.
you can try this,

for (int i = 0; i < list->count; i++)
{
     free(list->strings[i]);
}

free(list->strings);

Hope it helps!

Regards,
kotan
0
 

Author Comment

by:katjas
ID: 6388100
Wow .. it works !
Thank you
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
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 use strings and some functions related to them in the C programming language.

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now