衰减的多维数组从函数返回 [英] Decayed multidimensional array return from function

查看:92
本文介绍了衰减的多维数组从函数返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(gcc)多昏暗数组或用于无警告编译的双指针,是否有一种方法可以从函数中返回所谓的衰减数组指针"?总结(假设2个暗数组)返回int (*a)[5]格式而不是int**格式?

related to (gcc) Multi-Dim Array or Double Pointer for Warning-free Compile , is there a way to return so-called "decayed array pointer" from a function? in summary (suppose 2 dim array) returning int (*a)[5] format rather than int** format?

据我所知,当返回的int**指针发送到另一个等待(int*)[]参数的函数时,它无法正常工作.

as far as I see, when returned int** pointer is sent to another function waiting (int*)[] parameter, it is not working correctly.

推荐答案

是的,但是语法看起来不太好

Yes, but the syntax won't look nice

int(*f())[5] {
  static int a[1][5] = { { 1, 2, 3, 4, 5 } };
  return a;
}

基本上,这只是将您的a替换为f()(一个函数).使用typedef使其更具可读性

Basically, it's just your a replaced by f() (a function). Using a typedef it becomes more readable

typedef int inner_array[5];

inner_array *f() {
  // like before ...
}

请注意,要表示表象思维类型(不带名称),您需要编写int(*)[5].也就是说,您只需删除名称. (int*)[5]是无效的语法.

Notice that to denote the abstact type, without a name, you need to write int(*)[5]. That is, you just erase the name. (int*)[5] is not valid syntax.

您是对的-您不能返回int**,因为这意味着您有一个指向指针的指针.用f()[A][B]访问将从返回的指针给出的地址中读取,然后依次从该指针给出的地址中再次读取.但是实际上,您返回的数组指针仅指向一个内存块,因此,如果要进行两次间接访问,则将尝试将数据重新解释为指针.

You are right - you can't return int** because this means you have a pointer to a pointer. Accessing with f()[A][B] would read from the address the returned pointer gives, and then read again from the address given by that pointer in turn. But in fact, the array pointer you return points only to one memory block, so if you would make two indirections, you would try to reinterpret data as being pointers.

相反,如果返回int(*)[5]并执行f()[A][B],则不会从指针返回的地址中读取任何值.取而代之的是,您只需将偏移量A添加到地址,然后将类型从int(*)[5]调整为int[5],以使数组指向已调整地址处的内存.然后,下一个索引将再次按B进行调整,由于它先对int*进行操作(在数组衰减之后),而不再对数组指针进行操作,因此它将读取存储在调整后的地址上的内容,最终产生int.这是非数组指针和数组指针的重要区别.

Contrary, if you return a int(*)[5] and do f()[A][B] you will not read any value from the address returned by the pointer. Instead, you merely add the offset A to the address, and adjust the type from int(*)[5] to int[5] to have an array that refers to memory at the adjusted address. The next index will then again adjust by B and since it operates on a int* then (after the array decayed), not anymore on an array pointer, it will then read the content stored at the adjusted address, to finally yield the int. This is an important difference of non-array pointers and array pointers.

如果需要,您可以尝试一下.考虑这两个片段.一个可能会崩溃,但另一个可能不会崩溃(但两者都会产生未定义的行为,因此不应在实际程序中完成此操作)

You can experiment with this, if you want. Consider these two snippets. One will probably crash, but the other probably won't (but both yield undefined behavior, so this shouldn't be done in real programs)

int *pi = 0; 
int(*pa)[5] = 0;

int i = *pi; // read int stored at null-pointer address!
int *a = *pa; // does not read any data from address, just adjusts the type!

这篇关于衰减的多维数组从函数返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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