Avatar of highrate
highrate
 asked on

manipulating double arrays sent to function

Check the code, should be self explanitory what I'm trying to do.

The function crashes on line:

thespliced[i_word_num][i_word] = string[i_string];

Obviously because I haven't allocated memory for it.

How do allocate memory for a double array?
in the parse function:

char **spliced;
return_val = splice(string,thespliced);

int splice(char *string, char *thespliced[])
{


        int i_string=0;
        int i_word_num=0;
        int i_word=0;
        int i=0;


        for( ; i_string < (strlen(string) - 1) ; ++i_string)
        {


                if(string[i_string] == ' ')
                {
                        ++i_word_num;
                        i_word = 0;
                        continue;
                }
                thespliced[i_word_num][i_word] = string[i_string];
                ++i_word;
        }

        return i_word;
}

Open in new window

C

Avatar of undefined
Last Comment
phoffric

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
mccarl

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
highrate

ASKER
That's just the thing.  I know could do it that way, but I want the amount of array's to be
dynamic.  
mccarl

By dynamic do you mean, the array expands as you keep putting more items in it, or just that you can set the dimensions at runtime?

The first option doesn't exist in C. If you want something that expands you have to do it yourself by allocating a bigger array, copying over the data from the original smaller array into the new bigger array and then delete the original array.

The second option, you can do in 2 ways. Do you know the size of one of the dimensions? If so, you can do

  char spliced[20][] = malloc(20 * xDim * sizeof(char));

then you get an array with one fixed dimension and one dimension set at runtime.

If you want both dimensions to be able to be set at runtime, then you have to change a bit more code. You can no longer use the array as thespliced[3][6], because the actual address that that refers to is calculated at compile time and if you don't know one of the dimensions, the compiler can't work it out.

To do this, you have to declare your array as

  char spliced[] = malloc(xDim * yDim * sizeof(char));

and then pass it to your splice method as

  int splice(char *string, char thespliced[], int xDim)

and then you access the array like...
  thespliced[i * xDim + j];             // where i and j are the indexes into the array, and xDim is the size of the first dimension

Hope that helps
phoffric

Please post your parse and main functions.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
SOLUTION
phoffric

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question