Thread: pass multidimentional array to a function

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    pass multidimentional array to a function

    i need to pass a multi dimensional array to a function.

    if it was one dimensional i could do int *variablename

    everything i have found online says the function prototype should be like this

    myfunc( int [][26] ) and called with myfunc( name );

    This does not work i get the good old warning warning: passing argument 1 of ‘PrintGuesses’ from incompatible pointer type [-Wincompatible-pointer-types]|
    Last edited by cooper1200; 06-06-2023 at 12:58 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well that depends on how you declared your array.

    If you want to pass an array (of whatever dimension) to a function, just copy/paste the declaration of the array.
    Code:
    #include <stdio.h>
    
    void one ( int a[10] ) {
    }
    void two ( int b[5][2] ) {
    }
    void three( int c[3][4][5] ) {
    }
    
    int main() {
        int a[10];
        int b[5][2];
        int c[3][4][5];
        one(a);
        two(b);
        three(c);
        return 0;
    }
    Yes, you can faff around turning b[5][2] into b[][2] or worse (*b)[2], but you risk mucking up the edit.
    It's all the same to the compiler, you're not buying anything by doing it.

    Sure, some pedantic tools will carp about the most significant dimension being useless.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Oh, one more thing which seems to confuse noobs

    Whilst [] can decay to *
    [][] does NOT decay to **, but to (*)[]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    void PrintGuesses( char [3][26], int ) //function prototype
    PrintGuesses( Guesses, plen )// function call
    same warning....
    let me try and make a smaller version and post the proper code rather 150 lines of irreverent code

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    So the bloody thing works so i will have to paste original code sorry
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void Encrypt( char *, char *, char *, int, int );
    void Decrypt( char *, char * );
    void Get_Password( char *, char * );
    int StrLength( char * );
    int IsPrintable( int );
    void PrintGuesses( char [3][26], int );
    
    int main()
    {
        //char Text[] = "TOMORROW NEVER DIES";
        char Text[] = "The quick brown fox jumps, over the lazy dog!";
        char Pword[] = "hel";
        char Encrypted[sizeof Text];
    
        // Don't hardcode string lengths.
        int len = StrLength( Text );
        int plen = StrLength( Pword );
    
        printf("%s\n", Text);
    
        Encrypt( Text, Pword, Encrypted, plen, len );
    
        // Encrypted characters are not necessarily printable.
        for ( int i = 0; i < len; i++)
            printf(IsPrintable(Encrypted[i]) ? "%c" : "{0x%02X}", Encrypted[i]);
        putchar('\n');
    
        int Guesses[ sizeof Pword ][26] = { 0 };
        // Don't use numbers for letters, e.g., say 'A' instead of 65.
        for ( int StartIndex = 0; StartIndex < plen; StartIndex++ )
        {
            int GuessIndex = 0;
    
            for ( int i = 'a'; i <= 'z'; i++ )
            {
                int Letter = 1;
                for ( int j = StartIndex; j < len; j += plen ) {
                    int x = Encrypted[j] ^ i;
                    if ( ! ( ( x >= 'A' && x <= 'Z' ) || ( x >= 'a' && x <= 'z' ) || (  x >= ' ' && x <= '@' ) ) )
                    {
                        Letter = 0;
                        break;
                    }
                }
                if ( Letter )
                {
                    Guesses[StartIndex][GuessIndex] = i;
                    GuessIndex++;
                }
    
            }
            putchar('\n');
        }
    
        PrintGuesses( Guesses, plen );
    
        printf("%s\t%s\t%s\n", "1st", "2nd", "3rd");
        for ( int i = 0; ; i++ ) //first letter
        {
            if ( Guesses[0][i] == 0 )
                break;
            for ( int j = 0; ; j++ ) // second letter
            {
                if ( Guesses[1][j] == 0 )
                    break;
                for ( int k = 0; ; k++ ) //third letter
                {
                    if ( Guesses[2][k] == 0 )
                        break;
    
                    char GuessedPword[sizeof Pword] = { '\0' };
    
                    GuessedPword[0] = Guesses[0][i];
                    GuessedPword[1] = Guesses[1][j];
                    GuessedPword[2] = Guesses[2][k];
    
                    printf("%d\t%d\t%d = %s\n", i, j, k, GuessedPword);
                }
            }
        }
    
        return 0;
    }
    
    void Encrypt( char *Text, char *Pword, char *Encrypted, int Pwlen, int Txtlen )
    {
        for ( int j = 0, i = 0; Text[i]; i++, j = (j + 1) % Pwlen )
            Encrypted[i] = Text[i] ^ Pword[j];
        Encrypted[Txtlen] = '\0';
    }
    
    void Decrypt( char *Encrypted, char *Pword )
    {
        for ( int j = 0, i = 0; i < 20 ; i++ )
        {
            printf("%c", Encrypted[i] ^ Pword[j]);
    
            if ( j == 4 ) j = 0;
            else ++j;
        }
        putchar('\n');
    }
    
    void Get_Password( char *Encrypted, char *Text )
    {
        for ( int j = 0, i = 0; i < 20 ; i++, j++ )
        {
            printf("%c", Encrypted[i] ^ Text[j]);
        }
        putchar('\n');
    }
    
    int StrLength( char * str )
    {
        int Counter = 0;
    
        for ( int i = 0; str[i]; i++ )
            Counter++;
    
        return Counter;
    }
    
    int IsPrintable( int x )
    {
        return ( x > 32 && x < 127 )  == 1;
    }
    
    void PrintGuesses( char Guesses[3][26], int plen )
    {
        for ( int i = 0; i < plen; i++ )
        {
            printf("%d  ", i);
            for ( int j = 0; j < 26; j++ )
            {
                if ( Guesses[i][j] == 0 )
                    break;
    
                printf("%c ", Guesses[i][j]);
            }
            putchar('\n');
        }
    }

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    I don't think it's pedantic to suggest leaving the most significant dimension empty. It's not just that it's "useless", but that it can be misleading since the value isn't even checked so it can be different from the actual size. It's also very often the case that the most significant dimension is variable with it's size passed in, in which case there is no definite size.

    @B.D., sizeof Pword is 4, not 3 (it includes the '\0').
    Also, you need to declare Guesses consistently. You define it as int but pass it as char. Maybe just define it as char. (You should have got a warning about that; don't ignore warnings, fix them.)
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    the only warning i got is as above. but that is the second bloody stupid mistake i have made in as many days

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-20-2019, 02:00 PM
  2. Replies: 2
    Last Post: 01-10-2016, 01:23 AM
  3. Multidimentional array
    By Ducky in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2009, 01:29 PM
  4. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  5. Replies: 3
    Last Post: 04-02-2002, 01:39 PM

Tags for this Thread