What is wrong with the following code? How to fix this?

How to return a static multidimensional array of characters?

#include<stdio.h>
#include<conio.h>

#define SIZE 3

char ** MyFunction(void)
{
    static char arr[SIZE][SIZE]={
                                    {'A', 'B', 'C'},
                                    {'D', 'E', 'F'},
                                    {'G', 'H', 'I'}
                                };
    return arr;
}

void main(void)
{
    char ** arr1 = NULL;
    int i=0;
    int j=0;

    arr1 = MyFunction();

    for(i=0 ; i<SIZE ; i++)
    {
        for(j=0 ; j<SIZE ; j++)
        {
            printf("%c, ", arr1[i][j]);
        }

        printf("\n");
    }

    getch();
}
+3
source share
5 answers

First of all, arr cannot be char**. This is an address, but this is not a pointer address - this is the address of char [SIZE].

It has always been easier for me to handle them by dividing the sizes up with typedefs.

#define SIZE 3 
typedef char ROW[SIZE];   // a ROW type is an array of chars

ROW* MyFunction(void) 
{ 
     static ROW arr[SIZE]={  // arr is an array of ROW objects 
                                    {'A', 'B', 'C'}, 
                                    {'D', 'E', 'F'}, 
                                    {'G', 'H', 'I'} 
                                }; 
    return arr; 
} 

void main(void) 
{ 
    ROW* arr1 = NULL;
    // etc...
+4
source

Given an announcement

char arr[SIZE][SIZE];

then the type of expression arris equal char (*)[SIZE]; therefore the type of function must be

char (*myFunction(void))[SIZE] // myFunction returns a pointer to a SIZE-element
{                              // array of char
  static char arr[SIZE][SIZE] = ...;
  return arr;
}

Yes, defining the type of function is ugly. James's typedef version is easier to read, but that's what happens under the covers.

int main(void)
{
  char (*arr1)[SIZE];
  int i, j;

  arr1 = myFunction();
  ...
}
+1
source

char ** [] [] =...

0

?

, (, ) . .

, main ( ) arr1. main arr1 ( ) . .

#include <stdio.h>

enum{ SIZE = 3 };

void initArray( char arr[ SIZE ][ SIZE ] ) {

    char x = 'A';

    for( int i = 0; i < SIZE; i++ ) {
        for( int j = 0; j < SIZE; j++ ) {
            arr[ i ][ j ] = x;
            x += 1;
        }
    }
}

int main()
{
    char arr1[ SIZE ][ SIZE ];
    initArray( arr1 );

    for( int i = 0; i < SIZE; i++ ) {
        for( int j = 0; j < SIZE; j++ ) {
            printf("%c, ", arr1[ i ][ j ]);
        }
        printf("\n");
    }
}

, C.

. , , - (, enum #define, (i, j ), . , .

0
source

conio.h + getch () is not ANSI C. void main is not ANSI C. Take a new type, this will simplify.

#include<stdio.h>
#include<conio.h>

#define SIZE 3
typedef char CHAR3[SIZE];

CHAR3 *MyFunction(void)
{
    static CHAR3 arr[]={
                                    {'A', 'B', 'C'},
                                    {'D', 'E', 'F'},
                                    {'G', 'H', 'I'}
                                };
    return arr;
}

main(void)
{
    CHAR3 *arr1 = NULL;
    int i=0;
    int j=0;

    arr1 = MyFunction();

    for(i=0 ; i<SIZE ; i++)
    {
        for(j=0 ; j<SIZE ; j++)
        {
            printf("%c, ", arr1[i][j]);
        }

        printf("\n");
    }

   getch();
}
0
source

Source: https://habr.com/ru/post/1766833/


All Articles