Solved

How to implement stringlist ???

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

Independent Software Vendors: 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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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.

730 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