直接设置数组内容:
#include <stdio.h>
void main()
{
int i, j, t=0;
int a[3][4] = { {1,2,3,4},{5,6,7,8},{9,10,11,12} };
int b[12] = { 0 };
for (int i = 0; i < 3; i++)
{
for (int j = 0; j< 4; j++)
{
b[t] = a[i][j];
t++;
}
}
for (int t = 0; t < 12; t++)
{
printf("%d,", b[t]);
}
}
不直接设置数组:
#include<stdio.h>
void main()
{
int a[3][4], i, j;
int len1 = sizeof(a) / sizeof(a[0]);
int len2 = sizeof(a[0]) / sizeof(a[0][0]);
for (int i = 0; i < len1; i++)
{
for (int j = 0; j < len2; j++)
{
a[i][j] = i*len2 + j;
}
}
for (int i = 0; i < len1; i++)
{
for (int j = 0; j < len2; j++)
{
printf("%-4d", a[i][j]);
}
puts("");
}
puts("*****************************");
int b[12] = { 0 };
int k = 0;
for (int i = 0; i < len1; i++)
{
for (int j = 0; j < len2; j++)
{
b[k] = a[i][j];
k++;
}
}
for (k = 0; k < 12; k++)
{
printf("%d,", b[k]);
}
}
将二维数组c[3][4]转换成一维数组[12]
最新推荐文章于 2023-07-02 22:19:38 发布