• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

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

0
highrate
Asked:
highrate
  • 2
  • 2
2 Solutions
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Instead of

char **spliced;

use ...

char spliced[20][20];

(Or whatever dimensions that you need)
0
 
highrateAuthor Commented:
That's just the thing.  I know could do it that way, but I want the amount of array's to be
dynamic.  
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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
0
 
phoffricCommented:
Please post your parse and main functions.
0
 
phoffricCommented:
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:
    http://www.ibiblio.org/pub/languages/fortran/append-c.html
and this deals more with dynamic arrays:
    http://c-faq.com/~scs/cclass/int/sx9b.html   

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("\n\n");

   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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now