如何返回用C二维指针? [英] How to return a two-dimensional pointer in C?

查看:209
本文介绍了如何返回用C二维指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所暗示的,如何返回指针这样的:

As the title suggests, how to return pointer like this:

xxxxxxx foo() {

    static int arr[5][5];
    return arr;
}

BTW。我知道,我必须指定一个维度的至少规模,但如何?

BTW. I know that I must specify the size of one dimension at least, but how?

推荐答案

它有助于使用typedef为此:

It helps to use a typedef for this:

typedef int MyArrayType[][5];

MyArrayType * foo(void)
{
    static int arr[5][5];
    return &arr;   // NB: return pointer to 2D array
}

如果您不想使用一个因某种原因的typedef,或只是好奇什么上述功能的裸体版会是什么样子,那么答案是这样的:

If you don't want a use a typedef for some reason, or are just curious about what a naked version of the above function would look like, then the answer is this:

int (*foo(void))[][5]
{
    static int arr[5][5];
    return &arr;
}

希望你可以看到为什么使用一个typedef是这种情况下,一个好主意。

Hopefully you can see why using a typedef is a good idea for such cases.

这篇关于如何返回用C二维指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆