?
Solved

How to implement stringlist ???

Posted on 2001-08-15
3
Medium Priority
?
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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

Industry Leaders: 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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
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.

764 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