manipulating double arrays sent to function

Posted on 2011-03-07
Last Modified: 2012-05-11
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 = 0;
                thespliced[i_word_num][i_word] = string[i_string];

        return i_word;

Open in new window

Question by:highrate
  • 2
  • 2
LVL 35

Accepted Solution

mccarl earned 250 total points
ID: 35061639
Instead of

char **spliced;

use ...

char spliced[20][20];

(Or whatever dimensions that you need)

Author Comment

ID: 35061761
That's just the thing.  I know could do it that way, but I want the amount of array's to be
LVL 35

Expert Comment

ID: 35061913
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
LVL 32

Expert Comment

ID: 35065355
Please post your parse and main functions.
LVL 32

Assisted Solution

phoffric earned 250 total points
ID: 35066316
I think I understand your goals. I advise you to break down the problem into smaller pieces and ask separate questions until you get a better understanding of pointers and arrays.

I changed the prototypes to simplify the programming. All arrays are dynamically created. You can infer the prototypes from the usage.

One thing I did which may be of interest. I created an an extra function,
     int countNumTokens( char * myString );
to return the number of space delimited words in a phrase. This function allows you to determine how much space to allocate for your top-level array of pointers. Then in each element of this array, you need to allocate space for the particular token that you parsed out of your phrase.

Here is a tutorial on pointers and arrays:
and this deals more with dynamic arrays:   

You may wish to consider this test driver:
int main ()
   char phrase[] = "A rolling stone gathers no moss";
   char **tokens;
   int numTokens, tokenlen, k;

   tokens = parse(phrase, &numTokens );
   printf("Original phrase = \"%s\"\n", phrase); // notice phrase was not altered
   printf("\nNumber of words is %d\n", numTokens);
   printf( "\nlook at 2nd word one char at a time\n");

   tokenlen = strlen( tokens[1] );
   for( k=0; k < tokenlen; ++k ) {
      printf( "%c   ", tokens[1][k] );

   printf("Word List:\n");
   for( k=0; k<numTokens; ++k) {
      tokenlen = strlen( tokens[k] );
      printf("%2d: len=%2d:  %s\n", k, tokenlen, tokens[k] );
   freeMemory( tokens, numTokens ); // must free all mallocs
   return 0;

Open in new window

It produces this output:
Original phrase = "A rolling stone gathers no moss"

Number of words is 6

look at 2nd word one char at a time
r   o   l   l   i   n   g

Word List:
 0: len= 1:  A
 1: len= 7:  rolling
 2: len= 5:  stone
 3: len= 7:  gathers
 4: len= 2:  no
 5: len= 4:  moss

Open in new window

I recommend that you use a graphical debugger such as ddd to be able to view the memory layouts of your dynamic arrays.

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question