Using strsep () with a dynamic array of strings in C
I have the following code:
#include <string.h>
int main(void) {
char *buffer = NULL, **words = NULL, *aPtr = NULL, *sPtr;
int count = 0;
buffer = strdup("The quick brown fox jumps over the lazy dog");
sPtr = buffer;
do {
aPtr = strsep(&sPtr, " ");
words[count++] = ... // missing code
} while(aPtr);
return 0;
}
I am missing some code as you can see above ... Is there some kind of strdup () that works in this situation? The strdup () function itself doesn't work ... If it's not there, how can I make this piece of code work?
Pointer pointer is a headache for me ...
a source to share
As long as you have not yet identified words[0]
, words[1]
... so the use strdup
is not helping.
Worse, you don't know in advance how many words there will be, so there is no need for the malloc
space that you need.
One option is to replace it words
with a linked list or dynamic array.
a source to share
You need to allocate space for the array words
, for example:
#define MAX_NUM_WORDS 1024
...
/* on the stack */
char* words[MAX_NUM_WORDS];
...
/* or on the heap */
char** words;
if (( words = ( char** )malloc( MAX_NUM_WORDS * sizeof( char* ))) == NULL )
{
perror( "malloc" );
exit( 1 );
}
Or better yet, use a dynamic data structure like a singly linked list.
a source to share
you can use something simple if only for this:
#include <stdio.h>
#include <string.h>
#define MAX_WORDS 100
int main(void) {
char *buffer, *words[MAX_WORDS], *aPtr;
int count = 0, i;
buffer = strdup("The quick brown fox jumps over the lazy dog");
if (!buffer) return 1;
do {
aPtr = strsep(&buffer, " ");
if (aPtr && count < MAX_WORDS) words[count++] = aPtr;
} while(aPtr && count < MAX_WORDS);
for (i = 0; i < count; i++) {
printf("%s\n", words[i]);
}
return 0;
}
launch:
[macbook:~] jianlin $ ./a.out
The
quick
brown
fox
jumps
over
the
lazy
dog
[macbook:~] jianlin $
a source to share
Your first problem is that you want a list of words - you can get away with hard-coding some of the word restrictions (an array of N words, and each word is an address), but that's not really product quality. The real solution is to have a list (which can be implemented as a growing array inside)
I don't see a problem with strdup, but you must void strsep, the function doesn't have a good API. It would be easier to do what strsep does manually.
a source to share
You need a dynamically allocated array. You can roll your own, but why not use Seq_T
Dave Hanson's C Interfaces and Implementations ? The function Seq_addhi
will do exactly what you want.
a source to share
the loop above was a little awkward, so I changed it to:
#include <stdio.h>
#include <string.h>
#define MAX_WORDS 100
int main(void) {
char *buffer, *words[MAX_WORDS], *aPtr;
int count = 0, i;
buffer = strdup("The quick brown fox jumps over the lazy dog");
if (!buffer) return 1;
while((aPtr = strsep(&buffer, " ")) && count < MAX_WORDS)
words[count++] = aPtr;
for (i = 0; i < count; i++) {
printf("%s\n", words[i]);
}
return 0;
}
the output is the same.
a source to share