0

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);
Community
  • 1
  • 1
user3005945
  • 51
  • 5
  • 10
  • Enable more warnings, if you don't already have. Warnings are *very often* signs that you are doing something that you should not be doing. – Some programmer dude Nov 24 '13 at 13:26
  • Also, please post post a *complete* example, explaining things like what `Length` and `Count` is. Preferably a [SSCCE](http://sscce.org/). – Some programmer dude Nov 24 '13 at 13:28
  • And a last thing, it would probably be better to loop while `strtok` doesn't return `NULL`. E.g. `while (token != NULL) { ... }` – Some programmer dude Nov 24 '13 at 13:29
  • i added the 2 functions,and changed it to the while(even though in this case it does not relate).any other solution for the matter?i get a warning "passing argument 2 of 'StoreArr' from incompatible pointer type [enabled by default]" – user3005945 Nov 24 '13 at 13:42
  • i get a warning "passing argument 1,2 of 'StoreArr' from incompatible pointer type [enabled by default]" – user3005945 Nov 24 '13 at 13:57

1 Answers1

0

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • thank you very much,but i was aiming to solve my current code(As i would like to learn from the mistakes).as for the strtok changing the string - i do not mind,i simply need it to transfer the contents into the array.destination SHOULD be an array of strings(char arrays).cant i avoid allocation? – user3005945 Nov 24 '13 at 14:05
  • @user3005945 You can avoid explicit allocations by having an array of arrays, like `char Destination[X][Y];`, that's the only way. – Some programmer dude Nov 24 '13 at 14:07
  • okay.in case i do use allocation,how can i change my existing code?should i simply add an allocation for each cell?also,is my Destination considered an array of strings? – user3005945 Nov 24 '13 at 14:10
  • @user3005945 If `dest` already is a properly allocated array of strings, then just use `strdup` to duplicate each string. – Some programmer dude Nov 24 '13 at 14:12
  • okay.so my question is : how do i create a proper a properly allocated array of strings,and why should i duplicate each?i simply want to move each of the split words from the original string(char *) into each cell in the array.i do know the number of strings i need to store,that is why my destination array is set with a size.again,i appologize for the ignorance,i am a beginner in c and simply wanting to learn it from experiencing. – user3005945 Nov 24 '13 at 14:18
  • i edited my question with an allocation for the array,yet it still does not print anything.any thoughts? – user3005945 Nov 24 '13 at 14:42
  • can anyone possibly help?this is quite urgent.i keep getting passing argument 2 of 'StoreArr' from incompatible pointer type [enabled by default] from the calling(StoreArr(File,Destination);) – user3005945 Nov 24 '13 at 15:39