下面的代码有什么问题?如何纠正这个问题?

发布于 2024-09-25 06:55:29 字数 657 浏览 0 评论 0原文

如何返回静态多维字符数组?

#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();
}

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧人九事 2024-10-02 06:55:29

首先,arr 不能是 char**。它是一个地址,但不是指针的地址——它是 char[SIZE] 的地址。

我总是发现通过使用 typedef 分割维度来处理这些问题更容易。

#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...

First of all, arr can not be a char**. It is an address, but it's not the address of a pointer -- it's the address of a char[SIZE].

I've always found it easier to deal with these by spliting the dimensions up using 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...
鹿! 2024-10-02 06:55:29

给定声明

char arr[SIZE][SIZE];

后,表达式 arr 的类型为 char (*)[SIZE];因此,函数类型应该是

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

的,函数类型定义是丑陋的。 James 的 typedef 版本读起来更容易,但这就是幕后发生的事情。

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

  arr1 = myFunction();
  ...
}

Given the declaration

char arr[SIZE][SIZE];

then the type of the expression arr is char (*)[SIZE]; therefore, the function type should be

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

Yes, the function type definition is ugly. James' typedef version reads more easily, but this is what's happening under the covers.

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

  arr1 = myFunction();
  ...
}
成熟稳重的好男人 2024-10-02 06:55:29

更改为 static char ** arr[][]= ...

change to static char ** arr[][]= ...

风柔一江水 2024-10-02 06:55:29

谁拥有该阵列?

这非常重要,因为代码的某些部分必须拥有数组的完全所有权(创建、删除)。不过,代码的其他部分可以读写。

秉承这一理念,我将修改代码,以便 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 在大多数本地范围内),而有些程序主要用于与其他人类程序员进行通信,因此代码的可读性非常重要。

Who owns the array?

It is very important because some part of the code has to take full ownership (create, delete) of the array. Other parts of the code can read and write though.

With that philosophy, I would modify the code so that main function (in reality, it can be any function) takes ownership of arr1. main creates arr1, and passes it to other function (may be in other module) to "initialize" it. Here is a modified version of the program.

#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");
    }
}

Upon spending few minutes in searching, I found a good discussion on multi-dimensional array in C.

Another comment on the side. please note that I have done couple of other changes in the code, some are design (e.g. using enum instead of #define, declaring variables (i, j in most local scope) while some are formatting. Programs are mainly for communicating with other human programmers, so readability of code is very important.

吃颗糖壮壮胆 2024-10-02 06:55:29

conio.h + getch() 不是 ANSI C。
void main 不是 ANSI C。
采用新类型,它会更容易。

#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();
}

conio.h + getch() is not ANSI C.
void main is not ANSI C.
Take a new type, it make it easier.

#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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文