need help with array of string pointers
Author |
Message |
arie.. #1 / 8
|
 need help with array of string pointers
Hi all, As part of a bigger project, I need a way to set up an array consisting of string pointers. For now, I want to be able to parse a text file, seperating out each word and placing each word in a unique cell of the array. the word parser works fine. It has been malloced already and I can display each word on the screen. Is there an easy way to do this without using structs? If I do use a struct, where will I need to use malloc? here is what i have so far with questionsr: struct array_contents { char *word; Quote: };
typedef struct array_contents *contents; /*this is how I declare the array*/ contents word_array[ARRAY_SIZE] = {"\0"}; /*is this the right way to initially set all cells to null? I get a warning saying that they are different types*/ to copy the word into the array, do I just do something like this: word_array[i].word = w; or do I need to say word_array[i]->word = w; or do I need to use strcpy to do it? it's been a while since i've had to mess with pointers so I forgot how it works. Arie
|
Thu, 12 Feb 2004 01:59:34 GMT |
|
 |
Morris Dove #2 / 8
|
 need help with array of string pointers
Quote:
> Hi all, > As part of a bigger project, I need a way to set up an array > consisting of string pointers. For now, I want to be able to parse a > text file, seperating out each word and placing each word in a unique > cell of the array. the word parser works fine. It has been malloced > already and I can display each word on the screen.
Arie... You're welcome to look over some C that I wrote to do this kind of thing. It's located at http://www.iedu.com/mrd/c/tokenize.c and http://www.iedu.com/mrd/c/tokfile.c -- with any non-standard functions used lurking in the same directory. I don't think I filtered the tabs out of those files, so you might want to set tabs to 3 for viewing. HTH -- Morris Dovey West Des Moines, Iowa USA
|
Thu, 12 Feb 2004 02:28:06 GMT |
|
 |
Jens.Toerr.. #3 / 8
|
 need help with array of string pointers
Quote:
> As part of a bigger project, I need a way to set up an array > consisting of string pointers. For now, I want to be able to parse a
char *string_pointer_array[ MAKE_THIS_LONG_ENOUGH ]; and you have an array of string pointers. Or if you want to make its lengh runtime adjustable use char **spa; if ( ( spa = malloc( HOW_MANY_DO_YOU_NEED * sizeof( char * ) ) ) == NULL ) exit( EXIT_FAILURE ); Now you can store your strings either as references (if they have already malloc()ed somewhere else in your porgram) or you can malloc() memory for each string and then strcpy() it, for example for ( i = 0; i < max_strings; i++ ) { if ( ( spa[ i ] = malloc( strlen( old_string[ i ] ) + 1 ) ) == NULL ) exit( EXIT_FAILURE ); strcpy( spa[ i ], old_string[ i ] ); Quote: }
assuming you already have an array of string pointers called old_string of length max_strings (which must not be larger than HOW_MANY_DO_YOU_NEED). Quote: > text file, seperating out each word and placing each word in a unique > cell of the array. the word parser works fine. It has been malloced > already and I can display each word on the screen. > Is there an easy way to do this without using structs? > If I do use a struct, where will I need to use malloc? > here is what i have so far with questionsr: > struct array_contents > { > char *word; > }; > typedef struct array_contents *contents; > /*this is how I declare the array*/ > contents word_array[ARRAY_SIZE] = {"\0"}; > /*is this the right way to initially set all cells to null? I get a > warning saying that they are different types*/
Your new type contents is a pointer to such a structure, and you create an array of pointers to structures, not an array of structures. There is nothing yet that you can set to null (except the pointers which you could set to NULL). Before you do anything with strings within your structure you have to malloc() memory for the structures, then for the strings within your structure and then you can write something into these strings, e.g. using strcpy(). Quote: > to copy the word into the array, do I just do something like this: > word_array[i].word = w; > or do I need to say word_array[i]->word = w; > or do I need to use strcpy to do it?
It depends what you want to store in word entry of the structure, a copy of the string, in which case you need to malloc() memory first and then strcpy(), or just a reference to the place where the string is stored. Quote: > it's been a while since i've had to mess with pointers so I forgot how > it works.
You better take some time to figure it out again, because messing with pointers should only be done when you really understand what you're doing. Otherwise you will very likely get swamped in lots of hard to find segmentation faults or general protection faults, depending on which kind of system you're working on. Regards, Jens -- _ _____ _____
_ | | | | | | AG Moebius, Institut fuer Molekuelphysik | |_| | | | | | Fachbereich Physik, Freie Universitaet Berlin \___/ens|_|homs|_|oerring Tel: ++49 (0)30 838 - 53394 / FAX: - 56046
|
Thu, 12 Feb 2004 04:35:39 GMT |
|
 |
Barry Schwar #4 / 8
|
 need help with array of string pointers
See annotations Quote:
>Hi all, >As part of a bigger project, I need a way to set up an array >consisting of string pointers. For now, I want to be able to parse a >text file, seperating out each word and placing each word in a unique >cell of the array. the word parser works fine. It has been malloced >already and I can display each word on the screen. >Is there an easy way to do this without using structs?
If the only element in your struct is a char*, why bother with the struct at all. A simple array of char* will be just as effective Quote: >If I do use a struct, where will I need to use malloc?
Whether it is an array of struct or an array of char*, you only use malloc if you do not define the array as a variable in your program. If that is the case, then you define the appropriate type of pointer (either struct* or char**) and use malloc to allocate enough space for whatever size array you determine at run time. Quote: >here is what i have so far with questionsr: >struct array_contents >{ > char *word; >}; >typedef struct array_contents *contents; >/*this is how I declare the array*/ >contents word_array[ARRAY_SIZE] = {"\0"};
Ignoring the initialization for a moment: word_array is an array of ARRAY_SIZE objects of type contents. contents is an alias for type "pointer to struct array_contents". Therefore, word_array is an array of ARRAY_SIZE pointers to struct array_contents. Consider your initialization. By specifying only one initializer, you are saying initialize element 0 of the array to the value specified and set all the remaining elements (if any) to the appropriate form of the value zero. Since the array is an array of non-function pointers, the appropriate form of zero is NULL. That is OK for the remaining elements. Element 0 has a problem. Element 0 is a pointer to struct. The initializing value is a quoted string. (To be specific, the quoted string is converted by the compiler to an array of char. In this case, the array contains two elements: the first is a '\0' which you specified and the second is '\0' which the compiler automatically puts at the end of every quoted string.) In this context, the array of char will be treated as a pointer to char whose value is the address of the first character in the array. You end up trying to assign a char* to a variable that is expecting a struct*. This is not one of the automatic pointer conversions the compiler will do for you. If you want to set all the pointers to NULL, change your initialization to = {NULL}. Also note that none of the elements of word_array point to valid structures yet. Quote: >/*is this the right way to initially set all cells to null? I get a >warning saying that they are different types*/ >to copy the word into the array, do I just do something like this: >word_array[i].word = w;
Since word_array[i] is a pointer and not a structure, obviously not. Quote: >or do I need to say word_array[i]->word = w;
Since none of word_array[i] point to any valid structures, obviously not. Quote: >or do I need to use strcpy to do it?
This is a design question. Once you create a structure for word_array[i] to point to and put the structure address in word_array[i], you have two choices: If w is a char* that points to the start of the word and if the area where the word is currently located will not be reused, you could use the assignment. If w is a char* that points to the start of the word and if the word is nul terminated, you could allocate space for the word, store the address of the space in word_array[i]->word, and then strcpy the word to the new area. Quote: >it's been a while since i've had to mess with pointers so I forgot how >it works. >Arie
Using structures has added a level of indirection and confusion you don't need. Try something along the lines of: Allocate space for an array of char*. Keep track as you use them. When you need more, reallocate the array. As you isolate each word, put a '\0' behind it to make it a string. Allocate space to hold the word and put the address of this space in the next element of your char* array. Then strcpy the word from its present location (a buffer?) to the allocated space. You should end up with an array of char pointers, each pointing to a word. <<Remove the del for email>>
|
Thu, 12 Feb 2004 07:53:10 GMT |
|
 |
#5 / 8
|
 need help with array of string pointers
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
arie.. #6 / 8
|
 need help with array of string pointers
Thanks for clearing this up for me guys!
|
Thu, 12 Feb 2004 16:14:09 GMT |
|
 |
#7 / 8
|
 need help with array of string pointers
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
David Thompso #8 / 8
|
 need help with array of string pointers
... Quote: > >contents word_array[ARRAY_SIZE] = {"\0"}; ... > Consider your initialization. ... You end up trying to assign a > char* to a variable that is expecting a struct*. This is not one of > the automatic pointer conversions the compiler will do for you.
And wouldn't be safe if it did. A struct array_contents * can only validly point to memory that is large enough and correctly aligned for such a struct, and the pointer to 2 null characters generated by the string literal is not guaranteed and not certain to be either, plus is not allowed to be written to anyway. Quote: > If you want to set all the pointers to NULL, change your > initialization to > = {NULL}.
Or, if it is (as shown) a static-duration variable, leave the intializer out altogether; FAQ 1.30. -- - David.Thompson 1 now at worldnet.att.net
|
Sat, 21 Feb 2004 12:12:07 GMT |
|
|
|