Solved

How to implement stringlist ???

Posted on 2001-08-15
3
269 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
Comment Utility
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
Comment Utility
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
Comment Utility
Wow .. it works !
Thank you
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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 is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-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.

763 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

5 Experts available now in Live!

Get 1:1 Help Now