manipulating double arrays sent to function

Posted on 2011-03-07
Medium Priority
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 36

Accepted Solution

mccarl earned 1000 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 36

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 33

Expert Comment

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

Assisted Solution

phoffric earned 1000 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

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

589 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