|
From: Khalid El-K. <kha...@ho...> - 2002-12-24 20:16:35
|
Hi, I'm a newbie, just wanted to ask about the best way to make an array of strings (without using a string class) i mean using pointers "char *". I tried to make a pointer of pointer "char **" but i found that i'm unable to count the number of elements in this kind of work. Any help will be appreciated, thanx Regrads Khalid Al-kary http://creaturesx.ma.cx/kxparse/ _________________________________________________________________ Add photos to your e-mail with MSN 8. Get 3 months FREE*. http://join.msn.com/?page=features/featuredemail&xAPID=42&PS=47575&PI=7324&DI=7474&SU= http://www.hotmail.msn.com/cgi-bin/getmsg&HL=1216hotmailtaglines_addphotos_3mf |
|
From: Khalid El-K. <kha...@ho...> - 2003-03-31 00:04:45
|
Hi fellows, I'm developing a low-level C-based string manipulation API ... in my Implementation of the function "explode" which splits a string into pieces (with a specified separator) and returns these pieces into an array. The problem is, i need to initialize the returning array i knew the way to initialize the string array is: char *temp[5]; //where 5 is the number of array elements but ... for performance reasons, I need the number of elements (5 above) not to be specified from the begining, i mean i need to use something like realloc to resize this array on-the-fly ... Is there a possible way to do that fellows ? Thanx in advance :) Regards, Khalid Al-Kary _________________________________________________________________ Help STOP SPAM with the new MSN 8 and get 2 months FREE* http://join.msn.com/?page=features/junkmail |
|
From: Ioannis V. <iv...@em...> - 2003-03-31 01:15:46
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Khalid El-Kary > Sent: Monday, March 31, 2003 3:05 AM > To: Dev...@li... > Subject: [Dev-C++] array of strings > > > Hi fellows, > I'm developing a low-level C-based string manipulation API ... > in my Implementation of the function "explode" which splits a > string into > pieces (with a specified separator) and returns these pieces > into an array. > > The problem is, i need to initialize the returning array i > knew the way to > initialize the string array is: > > char *temp[5]; //where 5 is the number of array elements > > but ... for performance reasons, I need the number of > elements (5 above) not > to be specified from the begining, i mean i need to use > something like > realloc to resize this array on-the-fly ... > > Is there a possible way to do that fellows ? > > Thanx in advance :) You will have to use malloc(), calloc() and realloc() to allocate space on the free store. C99 provides the notion of variable length arrays which are built in arrays whose size can be defined at runtime (but cannot be changed during their lifetime). Dev-C++ (MINGW) supports C99 with the option -std=c99. Example of VLA: int size=7; int myvla[size]; and void whatever(int myvla[*]); Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL: http://run.to/noicys |
|
From: Daniel K. O. <dan...@ya...> - 2003-03-31 03:12:50
|
> Hi fellows, > I'm developing a low-level C-based string manipulation API ... > in my Implementation of the function "explode" which splits a > string into > pieces (with a specified separator) and returns these pieces > into an array. While you are reinventing the wheel, you can take a look at GLib's sources at www.gtk.org. More specifically the funciton g_strsplit(): http://developer.gnome.org/doc/API/2.0/glib/glib-String-Utility-Functions.ht ml#g-strsplit Here is the code: Note: "GSList" is a single linked list pointer. - 8< - 8< - 8< - 8< - 8< - 8< - 8< - 8< - 8< - 8< - 8< - 8< - 8< /** * g_strsplit: * @string: a string to split. * @delimiter: a string which specifies the places at which to split the string. * The delimiter is not included in any of the resulting strings, unless * @max_tokens is reached. * @max_tokens: the maximum number of pieces to split @string into. If this is * less than 1, the string is split completely. * * Splits a string into a maximum of @max_tokens pieces, using the given * @delimiter. If @max_tokens is reached, the remainder of @string is appended * to the last token. * * As a special case, the result of splitting the empty string "" is an empty * vector, not a vector containing a single string. The reason for this * special case is that being able to represent a empty vector is typically * more useful than consistent handling of empty elements. If you do need * to represent empty elements, you'll need to check for the empty string * before calling g_strsplit(). * * Return value: a newly-allocated %NULL-terminated array of strings. Use * g_strfreev() to free it. **/ gchar** g_strsplit (const gchar *string, const gchar *delimiter, gint max_tokens) { GSList *string_list = NULL, *slist; gchar **str_array, *s; guint n = 0; const gchar *remainder; g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (delimiter != NULL, NULL); g_return_val_if_fail (delimiter[0] != '\0', NULL); if (max_tokens < 1) max_tokens = G_MAXINT; remainder = string; s = strstr (remainder, delimiter); if (s) { gsize delimiter_len = strlen (delimiter); while (--max_tokens && s) { gsize len; gchar *new_string; len = s - remainder; new_string = g_new (gchar, len + 1); strncpy (new_string, remainder, len); new_string[len] = 0; string_list = g_slist_prepend (string_list, new_string); n++; remainder = s + delimiter_len; s = strstr (remainder, delimiter); } } if (*string) { n++; string_list = g_slist_prepend (string_list, g_strdup (remainder)); } str_array = g_new (gchar*, n + 1); str_array[n--] = NULL; for (slist = string_list; slist; slist = slist->next) str_array[n--] = slist->data; g_slist_free (string_list); return str_array; } - 8< - 8< - 8< - 8< - 8< - 8< - 8< - 8< - 8< - 8< Daniel K. O. Only ask questions with yes/no answers if you want "yes" or "no" as the answer. http://homepages.tesco.net/~J.deBoynePollard/FGA/questions-with-yes-or-no-an swers.html |
|
From: Ricardo R. <ri...@po...> - 2003-03-31 09:56:01
|
Here I send you an example of
a string array resizing on-the-fly as you require...
This possible solution is built with the
"realloc( )" function as you indicated in
your message.
I hope this example illuminates your way toward
a peaceful programming...Salam alecum!!.
Ricardo.
*******************PROGRAM*******************
/* ALLOCATING AND REALLOCATING MEMORY */
#include <stdio.h>
#include <stdlib.h>
int main()
{
char **p;
int N,i;
printf("Give me the initial array string dimension: ");
scanf("%d", &N);
fflush(stdin);
/* As you want your initial array dimension is given
by the user and it is allocated in this way */
p= (char **)realloc(NULL,N*sizeof(char*));
for(i=0; i<N ; i++)
{
/*strings of 10 char */
p[i]= (char*)realloc(NULL,10*sizeof(char));
printf("dame string %d: ",i);
gets(p[i]);
puts(p[i]);
}
/* if you need to store still more array strings
you can reuse the reallocate function as follows */
printf("Give me the new array string dimension: ");
scanf("%d",&N);
fflush(stdin);
p= (char **)realloc(p,N*sizeof(char*));
for(i=i; i<N ; i++)
{
p[i]= (char*)realloc(NULL,10*sizeof(char));
printf("dame string %d: ",i);
gets(p[i]);
puts(p[i]);
}
/* Verification of correct string storing */
printf("\n\n");
for(i=0; i<N ; i++) puts(p[i]);
/* At the end it is convenient to free the memory */
for(i=0; i<N ; i++) free(p[i]);
free(p);
system("PAUSE");
return 0;
}
*****************************
|
|
From: <or...@vp...> - 2002-12-25 11:54:11
|
Hello! Khalid El-Kary wrote: > Hi, > I'm a newbie, just wanted to ask about the best way to make an array of > strings (without using a string class) i mean using pointers "char *". > > I tried to make a pointer of pointer "char **" but i found that i'm > unable to count the number of elements in this kind of work. That's right, you cannot count the number of elements, if you created the array of char* pointers dynamically. You need another variable to store the number of elements. This is normal. -- Regards Balázs P.S.: Jesus was born! |
|
From: Brenden T. <br...@rc...> - 2002-12-26 20:22:59
|
Balazs is correct. However, there are a few other ways to do this. For
example, the C standard says that command line parameters will be passed
into main as a count, and as an array of pointers to stings, just as
Balazs said:
int main( int argc, char *argv[] ) {... }
argc is the ARGument Count, and argv is the ARGument Vector. However,
argv has one other little trick. It actually has argc + 1 vectors, and
the last vector is always NULL, so you can just ignore argc and count
through the strings untill you find the NULL one, and that is the end.
Obviously, this wouldn't work if some of your strings in the middle
have to be NULL, so watch out, but if they don't then it's a nice
alternative to having to maintain a seperate count, although a little
tricky to maintain. See K&R section "Command Line Arguments" more info
and nice pictures.
Also, you could have structure that has the count in the beginning and
then a variable sized char* vector after it. I believe gcc allows you
to declare variable sized vectors like this:
struct string_vect{
int count;
char *string[0];
} string_vect;
Then you have just one variable to pass into functions instead of two.
Again the maintainace cost is a little higher (you have to remeber to
allocate memory for this structure properly, i.e. malloc( sizeof (struct
string_vect) + sizeof (char* [10])) for ten strings for example, but
perhaps this would be better in the long run for you.
Good luck!
B.
P.S. I did NOT compile and check any of the code example given here.
Use at your own risk!
Khalid El-Kary wrote:
> Hi,
> I'm a newbie, just wanted to ask about the best way to make an array
> of strings (without using a string class) i mean using pointers "char *".
>
> I tried to make a pointer of pointer "char **" but i found that i'm
> unable to count the number of elements in this kind of work.
>
> Any help will be appreciated, thanx
>
> Regrads
> Khalid Al-kary
> http://creaturesx.ma.cx/kxparse/
>
>
> _________________________________________________________________
> Add photos to your e-mail with MSN 8. Get 3 months FREE*.
> http://join.msn.com/?page=features/featuredemail&xAPID=42&PS=47575&PI=7324&DI=7474&SU=
> http://www.hotmail.msn.com/cgi-bin/getmsg&HL=1216hotmailtaglines_addphotos_3mf
>
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
|