Solved

How to implement stringlist ???

Posted on 2001-08-15
3
272 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

839 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