Thread: Returning a multi-dimentional array from a function- how?

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Returning a multi-dimentional array from a function- how?

    I'm going to go mad... I've got a function with the type char, and I'm trying to return a multi-dimentional array at the end of the function:

    Code:
    char char_to_bin( char sOutput[1024] ); // thats my current prototyping
    
    int main ();
    {
    	char sOuput[1024];
    	//skip a lot of stuff
    	char_to_bin(sOutput);
    	return 0;
    }
    
    char char_to_bin( char sOutput[1024] )
    {
    	char sOutput2[1024][7];
    	//skip a lot of code... that part works
    	return sOutput2;
    }
    And I get this error:

    error C2440: 'return' : cannot convert from 'char [1024][7]' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast

    I can see what the problem is, but I have no clue how to fix it.

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Change
    Code:
    char char_to_bin( char sOutput[1024] )
    {
    	char sOutput2[1024][7];
    	//skip a lot of code... that part works
    	return sOutput2;
    }
    to
    Code:
    char **char_to_bin( char sOutput[1024] )
    {
    	char sOutput2[1024][7];
    	//skip a lot of code... that part works
    	return sOutput2;
    }

  3. #3
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Remember, it doesn't make sense to return an array. You are returning pointers to the array; so in your case you want to return a pointer to a pointer (2D array)

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    You could also declare a pointer in the function and dynamically allocate the memory and then return the pointer. Don't forget to delete it though.
    Code:
    void char_to_bin( char sOutput[1024] );
    
    int main () {
        char sOuput[1024];
        char **sOutput2;
    
        //skip a lot of stuff
    
        sOutput2 = char_to_bin( sOutput );
    
        for ( int i = 0; i < 1024; i++ )
              delete [] sOutput2[i];
    
        delete [] sOutput2;
    
        return 0;
    }
    
    char** char_to_bin( char sOutput[1024] )
    {
        char **sOutput2 = new char*[1024];
       
        for( int i = 0; i < 1024; i++ )
               sOutput2[i] = new char[7];
    
        //...
        //...
    
        return sOutput2;
    }
    This is example is pointless if the size is always 1024, but it's just another way of doing it. You should have a variable which stores how many char arrays you have so that you don't waste any memory.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I decided not to use the multidimentional array... now I've got another problem on another function...

    I need to pass 2 strings on to the function int XOR1()

    Code:
    int XOR1(char key[512], char string[4096]);
    
    int main()
    {
            XOR1("keygoeshere","stringgoeshere");
            return 0;
    }
    
    int XOR1(char key[100], char string[4096])
    {
             //code goes here
    }
    The console app crashes every time it calls XOR1.

    TEST1 caused an invalid page fault in
    module TEST1.EXE at 017f:004017bb.
    Registers:
    EAX=00000038 CS=017f EIP=004017bb EFLGS=00010202
    EBX=00570000 SS=0187 ESP=0067e338 EBP=0067e38c
    ECX=00000079 DS=0187 ESI=0043e2ca FS=663f
    EDX=0043e0b0 ES=0187 EDI=0067e38c GS=0000
    Bytes at CS:EIP:
    88 0a 8b 45 0c 03 45 f8 8a 08 51 b9 e0 47 44 00
    Stack dump:
    0067e57a 0043e2ca 00570000 cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc


    I don't know if using a pointer would fix this, and if so, how?

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Also, I'm having another problem that doesn't crash it in the function scramble...

    Here's what I want it to do:

    the left column represents the new string being assigned, the right is the source, before the digits are rearranged (that would work only if it was 15 characters long):

    Code:
    sOutput      Value
    sOutput[x]  sInput[x]
    0		1
    1		3
    2		5
    3		7
    4		9
    5		11
    6		13
    7		15
    8		0
    9		2
    10		4
    11		6
    12		8
    13		10
    14		12
    15		14
    For example: sOutput[1] has the same value as sInput[3]... My method for doing this is as follows:

    Code:
    void scramble()
    {
    	char sInput[1024];
    	char sOutput[1024];
    	int iLength = 0;
    	int i = 0, x = 0, y = 0, z = 0;
    
    	cout << "What message do you want to scramble?\n";
    	cin.get (sInput, 1024);
    	cin.ignore(1024, '\n');
    
    	iLength = strlen( sInput);
    	strrev(sInput);
    
    
    	x = 1;
    
    	for (i = 0; (i<iLength) && (sInput[x] != '\0'); i++)
    	{
    		sOutput[i] = sInput[x];
    		x = x + 2;
    	}
    
    	x = 0;
    
    	for (i; (i<iLength) && (sInput[x] != '\0'); i++)
    	{
    		sOutput[i] = sInput[x];
    		x = x + 2;
    	}
    
    	sOutput[i] = '\0';
    
    	cout << sOutput << endl << endl;
    
    	
    // 1 2 3 4 5 is an input example
    // 1 3 5 2 4 is the scrambled example
    // 4 2 5 3 1 is what it looks like after strrev
    }
    All that works fine... unless you input a string at least 10 characters (0123456789 won't work, but 123456789 will.)

    If you input 10 or more characters, it gives you mostly garbage characters.

    Also, I can't think of a simple way to descramble that so that 42531 becomes 13524 (strrev would work for that part), but then I need 13524 to be able to become 12345... so it would work both ways.
    Last edited by Trauts; 10-26-2002 at 03:07 PM.

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Eibro

    Code:
    char **char_to_bin( char sOutput[1024] )
    {
    	char sOutput2[1024][7];
    	//skip a lot of code... that part works
    	return sOutput2;
    }
    Returning a pointer to a local object is not a good thing.
    The memory needs to be allocated with new, as in The Dog's example.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Originally posted by Salem
    > XOR1("keygoeshere","stringgoeshere");
    string constants like this can be (and in your case are) in read-only memory. Which means your program dies if you try and change them

    char key[]="keygoeshere";
    char string[]="stringgoeshere";
    XOR1(key,string);
    How do I fix that in this case? I need an input string AND an output one.

  9. #9
    what do you mean you need an input and an output string?

    [CODE]
    char *key = "key";
    char *str = "string";
    encrypt(str,key)
    //.......
    char *(char *key, char *string)
    char *encrypted = new char[strlen(string)];
    /* code here */
    return encrypted;
    }
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I need it to store the output from the XORs in a string. I have to store it somewhere.

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Nevermind about that... I just won't use the function by itself.

    I need help on the other problem:

    Why will it rearrange strings with 9 digits: 123456789

    But not ones with 10 or more: 0123456789

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  5. multi array to function
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-09-2001, 03:01 AM