下面的代码有什么问题?如何纠正这个问题?
如何返回静态多维字符数组?
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,arr 不能是
char**
。它是一个地址,但不是指针的地址——它是 char[SIZE] 的地址。我总是发现通过使用 typedef 分割维度来处理这些问题更容易。
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.
给定声明
后,表达式
arr
的类型为char (*)[SIZE]
;因此,函数类型应该是的,函数类型定义是丑陋的。 James 的 typedef 版本读起来更容易,但这就是幕后发生的事情。
Given the declaration
then the type of the expression
arr
ischar (*)[SIZE]
; therefore, the function type should beYes, the function type definition is ugly. James' typedef version reads more easily, but this is what's happening under the covers.
更改为 static char ** arr[][]= ...
change to static char ** arr[][]= ...
谁拥有该阵列?
这非常重要,因为代码的某些部分必须拥有数组的完全所有权(创建、删除)。不过,代码的其他部分可以读写。
秉承这一理念,我将修改代码,以便
main
函数(实际上,它可以是任何函数)获得arr1
的所有权。main
创建arr1
,并将其传递给其他函数(可能位于其他模块中)以“初始化”它。这是该程序的修改版本。经过几分钟的搜索,我发现了一个 关于 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 ofarr1
.main
createsarr1
, and passes it to other function (may be in other module) to "initialize" it. Here is a modified version of the program.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.conio.h + getch() 不是 ANSI C。
void main 不是 ANSI C。
采用新类型,它会更容易。
conio.h + getch() is not ANSI C.
void main is not ANSI C.
Take a new type, it make it easier.