见代码(很久以前的):
本人实力不济,如有错误或建议及补充,请指出(评论或私信都行)
/*
码风很丑,见谅
可用于数组之间的赋值,节省代码量
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define m0(x) memset(x,0,sizeof(x))
int min(int x,int y){if(x<y) return x;else return y;}
int a[10]={0,1,2,3,4,5,6,7,8,9},
b[15]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14};
int a2[10][10];
/*
={
{0,1,2,3,4,5,6,7,8,9},
{10,11,12,13,14,15,16,17,18,19},
{20,21,22,23,24,25,26,27,28,29},
{30,31,32,33,34,35,36,37,38,39},
...
};
*/
int main()
{
//初始化
for(int i=0;i<10;i++) for(int j=0;j<10;j++) a2[i][j]=i*10+j;
int ans[10],ans2[10][10];
//1.直接全部赋值
m0(ans);
memcpy(ans,a,sizeof(a));//等效于 for(int i=0;i<10;i++) ans[i]=a[i];
for(int i=0;i<10;i++) printf("%d ",ans[i]);
printf("\n");
//1.1 记得检查一下,否则可能会RE
m0(ans);
memcpy(ans,b,sizeof(b));//应该写成 memcpy(ans,b,sizeof(ans)) 防止错误
for(int i=0;i<10;i++) printf("%d ",ans[i]);
printf("\n");
printf("\n\n\n");
system("pause");
system("cls");
//2. 二维数组
m0(ans2);
memcpy(ans2+1,a,sizeof(a));//赋给ans2[1]
for(int i=0;i<10;printf("\n",i++)) for(int j=0;j<10;j++) printf("%d ",ans2[i][j]);
printf("\n");
m0(ans2);
memcpy(ans2,a2,sizeof(a2));//同理
for(int i=0;i<10;printf("\n",i++)) for(int j=0;j<10;j++) printf("%d ",ans2[i][j]);
printf("\n");
return 0;
}
/*
\{^_^}/
*/
标签:私信,int,代码,memset,学习,include,库函数
From: https://www.cnblogs.com/AC-13-13/p/18475041