例如inta[2]={1,2};intb[2];如何将a数组的内容复制到b数组中?必须用for循环一个一个的进行复制么?...
例如int a[2]={1,2};
int b[2];
如何将a数组的内容复制到b数组中?必须用for循环一个一个的进行复制么?
#include"
string.h
"
#include"
stdio.h
"
intmain(void)
{
inti,j;
inta[2][3]={{1,2,3},{4,5,6}};
intb[2][3];
memcpy(&b[0][0],&a[0][0],24);
printf("%d",b[1][0]);
}
扩展资料
#include<stdio.h>
#include<string.h>
#include<
stdlib.h
>
voidprintarr2d(int(*a)[3],introw,intcol);
intmain()
{
inti,j;
inta[2][3]={{1,2,3},{4,5,6}};
intb[4][3]={{0,0,0},{0,0,0}};
memcpy(b[2],a,sizeof(int)*2*3);
printarr2d(b,4,3);
return0;
}
/***********************************************
打印显示数组
************************************************/
voidprintarr2d(int(*a)[3],introw,intcol)
{
inti,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
}
C语言中复制数组的内容源代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 10
void show_array(const int ar[], int n);
int main()
{
int values[SIZE] = {1,2,3,4,5,6,7,8,9,10};
int target[SIZE];
double curious[SIZE / 2] = \
{2.0, 2.0e5, 2.0e10, 2.0e20, 5.0e30};
puts("memcpy() used:");
puts("values (original data): ");
show_array(values, SIZE);
memcpy(target, values, SIZE * sizeof(int));
puts("target (copy of values):");
show_array(target, SIZE);
puts("\nUsing memmove() with overlapping ranges:");
memmove(values + 2, values, 5 * sizeof(int));
puts("values -- elements 0-5 copied to 2-7:");
show_array(values, SIZE);
puts("\nUsing memcpy() to copy double to int:");
memcpy(target, curious, (SIZE / 2) * sizeof(double));
puts("target -- 5 doubles into 10 int positions:");
show_array(target, SIZE/2);
show_array(target + 5, SIZE/2);
system("pause");
return 0;
}
void show_array(const int ar[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", ar[i]);
putchar('\n');
}
扩展资料
1、C语言编程中,将常用的操作封装成函数进行调用,可以大大简化程序的编写,而且在代码的维护性及可读性方面也提供了便利。
2、不同地方需要对处理后的数组内容多次进行显示,并且很多情况下并非显示数组里面的全部内容,而仅仅是想观察数组中的部分数据内容,若每次显示时都用printf函数写的话,可以写一个自定义的通用函数,用来根据需要显示数组中的内容。