C- struct的使用

时间:2023-03-08 22:28:17

数组是二等公民,不能进行整体赋值,或者参数传递,或者作为返回值。

But!如果封装在struct内部,就完全不一样了

 #include <iostream>
using namespace std; struct s_tag { int a[]; };
s_tag fa, sa, ua;
s_tag multiple(s_tag s) {
int j;
for(j = ; j < ; j++)
s.a[j] *= ;
return s;
} int main() {
for(int i = ; i < ; i++) fa.a[i] = i;
for(int i = ; i < ; i++) cout << fa.a[i] << " ";
sa = multiple(fa);
cout << endl;
for(int i = ; i < ; i++) cout << sa.a[i] << " ";
ua = sa;
cout << endl;
for(int i = ; i < ; i++) cout << ua.a[i] << " "; return ;
}

输出:

$ ./a.exe