我们可以 return 一个包含 C 内部数组的结构吗?
Can we return a structure containing an array inside in C?
这是一个结构:
struct elem {
int a[100];
int val;
};
elem foo() {
elem Result;
Result.a[0] = 5;
return Result;
}
int main () {
elem aux = foo();
//is Result passed back to aux, so that we can use its array?
cout<<aux.a[0]<<"\n"; // 5
}
我知道函数可以 return 简单的结构。
他们也可以 return 包含数组的结构吗?记忆中发生了什么?
还有:当我们声明 elem 结果时;在函数中,数组是用0初始化的,还是只取随机值?
是的,在 C 和 C++ 中都可以。
数组与结构的其余部分一起被复制element-by-element。这对于大型数组来说可能很慢。
is the array initialised with 0, or it just takes random values?
数组未初始化,因此包含不确定的值。访问它们会导致未定义的行为。
这是一个结构:
struct elem {
int a[100];
int val;
};
elem foo() {
elem Result;
Result.a[0] = 5;
return Result;
}
int main () {
elem aux = foo();
//is Result passed back to aux, so that we can use its array?
cout<<aux.a[0]<<"\n"; // 5
}
我知道函数可以 return 简单的结构。 他们也可以 return 包含数组的结构吗?记忆中发生了什么?
还有:当我们声明 elem 结果时;在函数中,数组是用0初始化的,还是只取随机值?
是的,在 C 和 C++ 中都可以。
数组与结构的其余部分一起被复制element-by-element。这对于大型数组来说可能很慢。
is the array initialised with 0, or it just takes random values?
数组未初始化,因此包含不确定的值。访问它们会导致未定义的行为。