質問

as a part of a program aiming to store a dictionary and replace words according to it,i wrote a function which will basically split(using strtok) a string to its words(seperated by spaces),and will store each word into an array of strings. The code is as follows:

void StoreArr(char * original,char ** dest)
            {
                int i=0;

                char * token =strtok(original, " ");
                dest[i]=malloc(sizeof(token));
                strcpy(dest[i],token);
                ++i;

                while(token!=NULL)
                {
                        dest[i]=malloc(sizeof(token));
                        strcpy(dest[i],token);
                        printf("%s",token);
                        token =strtok(NULL, " ");
                        ++i;
                }

            }

i passed the following variables:

         char * File = "";
         File=malloc(Length(Text)*(sizeof(char)));
         char ** Destination[Count(' ',File)];

the length of destination is the number of words. once the program is run,it terminates itself without displaying the text it is called using StoreArr(File,Destination);

edit:

int Length(FILE * file)
            {
                 long result;
                    long origPos = ftell(file);//original start position of file
                    fseek(file, 0, SEEK_END);//move to end of file
                    result = ftell(file);//return value at end
                    fseek(file, origPos, SEEK_SET);//rewind to beginning
                    return result;
            }
            int Count(char a,char * b)
            {
                int i=0;
                int count=0;
                while(b[i]!='\0')
                {
                if(b[i]==a || b[i]=='\n')
                    ++count;
                ++i;
                }
                return count+1;
            }

?i get a warning "passing argument 1,2 of 'StoreArr' from incompatible pointer type [enabled by default]" Thanks in advance.

p.s:Breaking down string and storing it in array

the code in the last post is the same as mine,yet mine does not work.i suspect those 2 lines form a problem,i do not know why:

 dest[i]=malloc(sizeof(token));
                    strcpy(dest[i],token);
役に立ちましたか?

解決

Without seeing more of your code, you look like you may have undefined behavior in your code.

With the declaration of Destination you have an array of pointers to a pointer, and I don't know if that's what you want. You also need to allocate all the pointers.

You also have to be careful as strtok modifies the string it tokenizes, so you can't pass a literal or constant string.


If it was me, I would do something like this instead:

char **Destination = NULL;
StoreArr(Original, &Destination);

And have the function like this

size_t StoreArr(char *original, char ***destination)
{
    if (original == NULL || destination == NULL)
        return 0;

    size_t size = 0;
    char *token = strtok(original, " ");
    while (token != NULL)
    {
        /* (re)allocate array */
        char **temp = realloc(*destination, sizeof(char *) * (size + 1));
        if (temp == NULL)
        {
            /* Allocation failed, free what we have so far */
            if (*destination != NULL)
            {
                for (size_t i = 0; i < size; ++i)
                    free((*destination)[i]);
                free(*destination);
            }

            return 0;
        }

        /* Set the destination pointer */
        *destination = temp;

        /* Duplicate the token */
        (*destination)[size++] = strdup(token);

        /* Get next token */
        token = strtok(NULL, " ");
    }

    return size;
}

The function now dynamically allocates new entries as needed, and returns the number of entries in the array or 0 on error.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top