0

Well, I declared a global array of chars like this char * strarr[];

in a method I am tokenising a line and try to put everything into that array like this

*line =  strtok(s, " ");

while (line != NULL) {
  *line = strtok(NULL, " ");
}

seems like this is not working.. How can I fix it?

Thanks

yan
  • 20,644
  • 3
  • 38
  • 48
MattD
  • 35
  • 1
  • 3
  • 1
    Can you provide more context of the code, and more details of what you're seeing as the problem? – chmeee Aug 30 '11 at 02:09
  • it says tentative array definition assumed to have one element... – MattD Aug 30 '11 at 02:11
  • 1
    @MattD - Then what is the point of having array of pointers. Just `char*` would be sufficient. You don't even know how many tokens are going to present in a sequence to have an array to hold pointers to these tokens. – Mahesh Aug 30 '11 at 02:12
  • 1
    Note that your array is actually of pointers to char, not of chars. The difference is important. – Tom Zych Aug 30 '11 at 02:16
  • You have left out many important details. Please provide a complete, minimal example program. For more information about how to produce such a program, and why it is important, please see http://sscce.org/. – Robᵩ Aug 30 '11 at 02:25

1 Answers1

2

Any number of things could be going wrong with the code you haven't shown us, such as undefined behaviour by strtoking a string constatnt, or getting your parameters wrong when calling the function.

But the most likely problem from the code we can see is the use of *line instead of line, assuming that line is of type char *.

Use the following code as a baseline:

#include <stdio.h>
#include <string.h>

int main (void) {
    char str[] = "My name is paxdiablo";

    // Start tokenising words.

    char *line =  strtok (str, " ");
    while (line != NULL) {
        // Print current token and get next word.

        printf ("[%s]\n", line);
        line = strtok(NULL, " ");
    }

    return 0;
}

This outputs:

[My]
[name]
[is]
[paxdiablo]

and should be easily modifiable into something you can use.

Be aware that, if you're trying to save the character pointers returned from strtok (which would make sense for using *line), they are transitory and will not be what you expect after you're done. That's because modifications are made in-place within the source string. You can do it with something like:

#include <stdio.h>
#include <string.h>

int main (void) {
    char *word[4];         // The array of words.
    size_t i;              // General counter.
    size_t nextword = 0;   // For preventing array overflow.

    char str[] = "My name is paxdiablo";

    // Start tokenising.

    char *line =  strtok (str, " ");
    while (line != NULL) {
        // If array not full, duplicate string to array and advance index.

        if (nextword < sizeof(word) / sizeof(*word))
            word[nextword++] = strdup (line);

        // Get next word.

        line = strtok(NULL, " ");
    }

    // Print out all stored words.

    for (i = 0; i < nextword; i++)
        printf ("[%s]\n", word[i]);

    return 0;
}

Note the specific size of the word array in that code above. The use of char * strarr[] in your code, along with the message tentative array definition assumed to have one element is almost certainly where the problem lies.

If your implementation doesn't come with a strdup, you can get a reasonably-priced one here :-)

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • what if char str is outside the main method? – MattD Aug 30 '11 at 02:15
  • @MattD, `strok` would modify `str`. I guess you may want to make a copy of it (`strcpy`). – J-16 SDiZ Aug 30 '11 at 02:25
  • can I not simple declare an empty array char arr[] and fill it up afterwards?? – MattD Aug 30 '11 at 02:40
  • @MattD: no, what you have there is an incomplete type. The reason you can get away with that for `argv` is because the startup code has already done the grunt work for you, setting `argc` to the size and even `argv[argc]` to null. There is no such setup for `char xyz[]`, the compiler literally doesn't know how big that should be. – paxdiablo Aug 30 '11 at 02:54