#include"string.h"
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、重慶小程序開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了萬年免費建站歡迎大家使用!
#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]);
}
擴展資料
#includestdio.h
#includestring.h
#includestdlib.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;
}
/***********************************************
打印顯示數(shù)組
************************************************/
voidprintarr2d(int(*a)[3],introw,intcol)
{
inti,j;
for(i=0;irow;i++)
{
for(j=0;jcol;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
}
strcpy(t[i],a[j],n);該語句的意思是:將某已知二維數(shù)組a的第j行前n個字符復(fù)制到另一個二維數(shù)組t的第i行中。給分吧
gcc 編譯測試通過
#include?stdlib.h
#include?stdio.h
#define?N?10
int?*?copyArray(int?*source,?int?n)
{
int?*dest;
int?i;
//?分配空間
dest?=?(int*)malloc(n?*?sizeof(int));
//?順序復(fù)制
for(i?=?0;i??n;i?++)
dest[i]?=?source[i];
return?dest;
}
int?*copyReverse(int?*source,?int?n)
{
int?*dest;
int?i;
//?分配空間
dest?=?(int*)malloc(n?*?sizeof(int));
//?逆序復(fù)制
for(i?=?0;i??n;i?++)
dest[n?-?i?-?1]?=?source[i];
return?dest;
}
int?*copyOrder(int?*source,?int?n)
{
int?*dest;
int?i,j,minIndex;
//?分配空間
dest?=?(int*)malloc(n?*?sizeof(int));
//?順序復(fù)制
for(i?=?0;i??n;i?++)
dest[i]?=?source[i];
//?對數(shù)組選擇排序
for(i?=?0;i??n?-?1;i?++)
{
minIndex?=?i;
for(j?=?i;j??n;j?++)
{
//?選擇本次最小下標(如果需要降序,將??改為??,重新編譯)
if(dest[j]??dest[minIndex])
minIndex?=?j;
//?交換元素
if(minIndex?!=?i)
{
dest[i]?=?dest[i]?^?dest[minIndex];
dest[minIndex]?=?dest[i]?^?dest[minIndex];
dest[i]?=?dest[i]?^?dest[minIndex];
}
}
}
return?dest;
}
int?main()
{
int?test[N]?=?{2,4,1,0,9,5,6,8,7,3};
int?*origin,*reverse,*order;
int?i;
origin?=?copyArray(test,N);
reverse?=?copyReverse(test,N);
order?=?copyOrder(test,N);
for(i?=?0;?i??N;?i?++)
printf("%d?",origin[i]);
printf("\n");
for(i?=?0;?i??N;?i?++)
printf("%d?",reverse[i]);
printf("\n");
for(i?=?0;?i??N;?i?++)
printf("%d?",order[i]);
printf("\n");
free(origin);
free(reverse);
free(order);
return?0;
}
#include?stdio.h
#includestring.h
int?main()
{
char?ch[40];
char?c[20]={"asdfgh"};
strcpy(ch,c);
printf("%s\n",ch);
}
有兩種常用的方法。
1 對數(shù)組各個維循環(huán),遍歷每個元素,并將其賦值到目標數(shù)組的對應(yīng)位置上。
缺點:代碼相對復(fù)雜。
優(yōu)點:可以不不同大小和形式的數(shù)組進行交叉復(fù)制。
2 利用C語言中多維數(shù)組元素存儲連續(xù)性,使用memcpy函數(shù)整體復(fù)制。
缺點:僅使用源數(shù)組要復(fù)制的數(shù)據(jù)是連續(xù)的,同時在目標數(shù)組中以同樣順序連續(xù)復(fù)制的情況。
優(yōu)點:代碼簡單,一個函數(shù)調(diào)用即可完成賦值。相對第一種,執(zhí)行效率略高。
C++ 復(fù)制函數(shù)
一、場景一:全部默認
#include
class base
{
public:
base()
{
}
base(int dt):
data(dt){
}
void get()
{
printf("base::data = %d\n",data);
}
public:
int data;
};
class derived:public base
{
public:
derived()
{
}
derived(int dt,int ddt):
base(dt),ddata(ddt)
{
}
void get()
{
base::get();
printf("derived::ddata = %d\n",ddata);
}
private:
int ddata;
};
int main()
{
derived da(1,5);
derived db(2,10);
da.get();
db.get();
da = db;
da.get();
db.get();
derived dc(da);
dc.get();
base ba(da);
ba.get();
getchar();
}
場景二、基類默認,派生類自定義
#include
class base
{
public:
base()
{
}
base(int dt):
data(dt)
{
}
void get()
{
printf("base::data = %d\n",data);
}
public:
int data;
};
class derived:
public base
{
public:
derived()
{
}
derived(int dt,int ddt)
:base(dt),ddata(ddt)
{
}
derived(const derivedd)
{
printf("call derived::copy construct function.\n");
ddata = d.ddata;
}
derived operator=(const derivedd)
{
printf("call derived::operator= function.\n");
ddata = d.ddata;
return *this;
}
void get()
{
base::get();
printf("derived::ddata = %d\n",ddata);
}
private:
int ddata;
};
場景三、基類自定義,派生類默認
#include
class base
{
public:
base()
{
}
base(int dt):
data(dt)
{
}
base(const baseb)
{
printf("call base::copy construct function.\n");
data = b.data;
}
base operator=(const based)
{
printf("call base::operator= function.\n");
data = d.data;
return *this;
}
void get()
{
printf("base::data = %d\n",data);
}
public:
int data;
};
class derived:public base
{
public:
derived()
{
}
derived(int dt,int ddt):
base(dt),ddata(ddt)
{
}
void get()
{
base::get();
printf("derived::ddata = %d\n",ddata);
}
private:
int ddata;
};
場景四、基類派生類都自定義
#include
class base
{
public:
base()
{
}
base(int dt):
data(dt)
{
}
base(const baseb)
{
printf("call base::copy construct function.\n");
data = b.data;
}
base operator=(const based)
{
printf("call base::operator= function.\n");
data = d.data;
return *this;
}
void get()
{
printf("base::data = %d\n",data);
}
public:
int data;
};
class derived:public base
{
public:
derived()
{
}
derived(int dt,int ddt):
base(dt),
ddata(ddt)
{
}
derived(const derivedd):base(d)
{
printf("call derived::copy construct function.\n");
ddata = d.ddata;
}
derived operator=(const derivedd)
{
printf("call derived::operator= function.\n");
ddata = d.ddata;
return *this;
}
void get()
{
base::get();
printf("derived::ddata = %d\n",ddata);
}
private:
int ddata;
};