?
Solved

manipulating double arrays sent to function

Posted on 2011-03-07
5
Medium Priority
?
209 Views
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_num;
                        i_word = 0;
                        continue;
                }
                thespliced[i_word_num][i_word] = string[i_string];
                ++i_word;
        }

        return i_word;
}

Open in new window

0
Comment
Question by:highrate
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 36

Accepted Solution

by:
mccarl earned 1000 total points
ID: 35061639
Instead of

char **spliced;

use ...

char spliced[20][20];

(Or whatever dimensions that you need)
0
 

Author Comment

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

Expert Comment

by:mccarl
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
0
 
LVL 32

Expert Comment

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

Assisted Solution

by:phoffric
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:
    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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

771 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