Copy multidimensional array to single array?

This is a discussion on Copy multidimensional array to single array? within the C Programming forums, part of the General Programming Boards category; Hi! Been working all day trying to copy this multidimensional array to a single dimensional array. Everytime I try I ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    4

    Copy multidimensional array to single array?

    Hi!
    Been working all day trying to copy this multidimensional array to a single dimensional array. Everytime I try I either get a segmentation fault or it tries to access some wierd memory stuff

    Here's the code if you'd like to check it. It is using OpenSSL to hash a text so don't mind the OpenSSL code (I know that part is working)

    Code:
    char *EncryptString(const char *encryption, const char *string) {
       EVP_MD_CTX mdctx;
       const EVP_MD *md;
       unsigned char mdv[EVP_MAX_MD_SIZE];
       int md_len;
    
       OpenSSL_add_all_digests();
       md = EVP_get_digestbyname(encryption);
    
       EVP_MD_CTX_init(&mdctx);
       EVP_DigestInit_ex(&mdctx, md, NULL);
       EVP_DigestUpdate(&mdctx, string, strlen(string));
       EVP_DigestFinal_ex(&mdctx, mdv, &md_len);
       EVP_MD_CTX_cleanup(&mdctx);
    
       char mdv_temp[255][5];
       char r_string[512];
    
       int i=0;
    
       for(i=0;i<md_len;i++) {
          sprintf(mdv_temp[i],"%02x",mdv[i]);
       }
    
       for(i=0;i<255;i++) {
          r_string[i] = mdv_temp[i];
       }
    }
    What i want to know is if there is any cool (read: good) way to do this (You are free to tell me how) and/or what I am doing wrong.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What exactly are you trying to do? First off, 'r_string' is too small. It needs to be 255 * 5 characters in length. 255 * 4 + 1, if you strip all of the null characters off of each string, assuming they're all strings. Then just loop through and strcat each line, assuming that they are all actually strings.
    Code:
    char mdv_temp[ 255 ][ 5 ];
    char r_string[ 255 * 4 + 1 ] = { 0 };
    size_t x;
    
    /* ...do stuff... */
    
    for( x = 0; x < 255; x++ )
    {
        strcat( r_string, mdv_temp[ x ] );
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    4
    What i want to do is to copy mdv_string to r_string (Where mdv_string is a multidimensional array).

    Your example won't copy the null termination, right?

  4. #4
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    It won't copy the NULs but there will be one at the end when it's finished copying.
    And shouldn't the size of r_string be 255*5 - 4?

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    4
    but i'm keep getting that strange output (like it tries to access memory it shouldn't)

    EDIT: I just noticed that the output i want is in the beginning of that weird output :P
    Last edited by seepox; 05-07-2006 at 07:27 PM.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Copying a multidementional array into a single dementional array is easy. Simply treat the multidementional array as a single dementional array, using multiArray[0] where you need the name of the array. If your compiler will not let you do this, or issues a warning you could use a pointer to multiArray[0][0] to achieve the same effect.

    For concatanating a multidementional array of strings, it is better to use strcat() to get rid of all the null characters.


    ... If you're printing strange output at the end of the expected output, you're probably missing a null character.
    Last edited by King Mir; 05-07-2006 at 09:06 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    4
    Thanks for the info, but how could i copy the null sign too? I played around with adding it to the end of the array and it didn't give me the weird output anymore (just the output i wanted). Maybe not just copy it, but at least get the position where i should add it.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by OnionKnight
    It won't copy the NULs but there will be one at the end when it's finished copying.
    And shouldn't the size of r_string be 255*5 - 4?
    No. Why would it be that?

    There are 255 strings, each 5 characters long, including the null. Subtract one from each one, for the null, multiply by the number of strings, and add one, so that you can null terminate.

    size = (255 * (5 - 1)) + 1

    As for the rest of you who are lost in confusion, read what I wrote before, and go on your merry way.

    Why is it no one listens to anything I say?


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,635
    I listen to you quzah.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Quote Originally Posted by seepox
    Thanks for the info, but how could i copy the null sign too? I played around with adding it to the end of the array and it didn't give me the weird output anymore (just the output i wanted). Maybe not just copy it, but at least get the position where i should add it.
    Just copy each value using this:
    Code:
    for(i=0;i<max;i++)
          singleArray[i]=multiArray[0][i];
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making a copy of an array by copying its address..
    By transgalactic2 in forum C Programming
    Replies: 12
    Last Post: 04-20-2009, 11:07 AM
  2. Need loop to copy chunks from byte array
    By jjamjatra in forum C# Programming
    Replies: 2
    Last Post: 03-05-2009, 04:42 AM
  3. Single line user inputted string to Array?
    By Sparrowhawk in forum C Programming
    Replies: 11
    Last Post: 12-14-2008, 07:10 PM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. Help to copy Array a into first portion of array b
    By Anna Lane in forum C Programming
    Replies: 4
    Last Post: 11-25-2002, 08:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21