c語言memcpy函數
『壹』 求助!c語言裡面的memcpy函數的用法
pascal語言不會
不過意思就是把b數組裡面的長度為b數組大小的數據復制到a數組裡面去
實際效果應該等同於下面的代碼
void*memcpy(void*destination,constvoid*source,size_tnum){
size_ti;
char*dest=(char*)destination;
constchar*sour=(constchar*)source;
for(i=0;i<num;i++){
dest[i]=sour[i];
}
returndestination;
}
『貳』 memcpy函數怎麼用
c和c++使用的內存拷貝函數,memcpy函數的功能是從源src所指的內存地址的起始位置開始拷貝n個位元組到目標dest所指的內存地址的起始位置中。
從源src所指的內存地址的起始位置開始拷貝n個位元組到目標dest所指的內存地址的起始位置中
C語言中使用#include <string.h>;
C++中使用#include <cstring>和#include <string.h>都可以。
1.source和destin所指的內存區域可能重疊,但是如果source和destin所指的內存區域重疊,那麼這個函數並不能夠確保source所在重疊區域在拷貝之前不被覆蓋。而使用memmove可以用來處理重疊區域。函數返回指向destin的指針.
2.如果目標數組destin本身已有數據,執行memcpy()後,將覆蓋原有數據(最多覆蓋n)。如果要追加數據,則每次執行memcpy後,要將目標數組地址增加到你要追加數據的地址。
注意:source和destin都不一定是數組,任意的可讀寫的空間均可。
『叄』 C語言中memcpy函數用法
Visual C++把memcpy和memmove實現的一樣,即不用擔心覆蓋的問題,這個可以看VC安裝目錄里的crt源碼得知。
至於gcc,沒有看過glibc的源碼。
『肆』 C語言 實現逆序的Memcpy方法。
參考代碼如下:
void*reversememcpy(void*destination,constvoid*source,intnum)
{
char*des=(char*)destination,*src=(char*)source;
inti;
if(des==NULL||src==NULL||num<=0){
printf("error");
returnNULL;
}
for(i=0;i<num;++i)
des[num-i-1]=src[i];
return(void*)des;
}
『伍』 C語言,memcpy函數的作用,最好再給個小程序~
函數名: memcpy
功 能: 從源source中拷貝n個位元組到目標destin中
用 法: void *memcpy(void *destin, void *source, unsigned n);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char src[] = "******************************";
char dest[] = "";
char *ptr;
printf("destination before memcpy: %s\n", dest);
ptr = memcpy(dest, src, strlen(src));
if (ptr)
printf("destination after memcpy: %s\n", dest);
else
printf("memcpy failed\n");
return 0;
}
『陸』 C語言串拷貝(strcpy)和內存拷貝(memcpy)函數有什麼不同
strcpy()函數只能拷貝字元串。strcpy()函數將源字元串的每個位元組拷貝到目錄字元串中,當遇到字元串末尾的null字元(\0)時,它會刪去該字元,並結束拷貝。 memcpy()函數可以拷貝任意類型的數據。因為並不是所有的數據都以null字元結束,所以你要為memcpy()函數指定要拷貝的位元組數。 在拷貝字元串時,通常都使用strcpy()函數;在拷貝其它數據(例如結構)時,通常都使用memcpy()函數。以下是一個使用strcpy()函數和memcpy()函數的例子: #include <stdio. h> #include <string. h> typedef struct cust-str {int id ;char last_name [20] ; char first_name[l5];} CUSTREC;void main (void); void main (void){char * src_string = "This is the source string" ; char dest_string[50]; CUSTREC src_cust; CUSTREC dest_cust; printf("Hello! I'm going to src_string into dest_string!\n"); / * Copy src_ string into dest-string. Notice that the destination string is the first argument. Notice also that the strcpy() function returns a pointer to the destination string. * / printf("Done! dest_string is: %s\n" , strcpy(dest_string, src_string)) ; printf("Encore! Let's one CUSTREC to another. \n") ; prinft("I'll src_cust into dest_cust. \n"); / * First, intialize the src_cust data members. * / src_cust. id = 1 ; strcpy(src_cust. last_name, "Strahan"); strcpy(src_cust. first_name, "Troy"); / * Now, Use the memcpy() function to the src-cust structure to the dest_cust structure. Notice that, just as with strcpy(), the destination comes first. * / memcpy(&dest_cust, &src_cust, sizeof(CUSTREC));
『柒』 求解,C語言中memcpy函數有問。
char *destin="abcdefghijklmn";
這樣表示的是一個字元串 常量 ,是不允許改變destin中的內容的。
destin[] 聲明的是一個數組,是可以改變裡面內容的。
『捌』 c語言 memcpy函數的作用
內存拷貝。常用來對數組進行復制。
與strcpy的區別在於需要設置位元組數。
詳見MSDN
『玖』 求助,C語言中函數memset(),memcpy()和strchr()的功能和用法
memset函數用來對一段內存空間全部設置為某個字元,常用於內存空間初始化。將已開辟內存空間
s
的首
n
個位元組的值設為值
c
。
下面是一個例子
#include
<stdio.h>
#include
<string.h>
main(){
char
*s="Golden
Global
View";
clrscr();
memset(s,'G',6);
printf("%s",s);
getchar();
return
0;
}
C語言memcpy函數原型:extern
void
*memcpy(void
*dest,
void
*src,
unsigned
int
count);
用法:#include
<string.h>
功能:由src所指內存區域復制count個位元組到dest所指內存區域。
說明:src和dest所指內存區域不能重疊,函數返回指向dest的指針。
舉例:
//
memcpy.c
#include
<syslib.h>
#include
<string.h>
main()
{
char
*s="Golden
Global
View";
char
d[20];
clrscr();
memcpy(d,s,strlen(s));
d[strlen(s)]=0;
printf("%s",d);
getchar();
return
0;
}
函數
strchr()
功
能:
在一個串中查找給定字元的第一個匹配之處\
用
法:
char
*strchr(char
*str,
char
c);
程序例:
#include
#include
int
main(void)
{
char
string[15];
char
*ptr,
c
=
'r';
strcpy(string,
"This
is
a
string");
ptr
=
strchr(string,
c);
if
(ptr)
printf("The
character
%c
is
at
position:
%d\n",
c,
ptr-string);
else
printf("The
character
was
not
found\n");
return
0;
}
『拾』 C語言memcpy的一些問題
雖然不少書說int和char是相通的但實際上現在的形式是int是佔四個位元組,而char是佔1個位元組。memcpy是字元串處理函數。所以你這樣直接用int的地址是錯誤的,int和char不是不可以通用。只是一般很容易搞錯。一般通用的值恐怕只有0這個值了。因為0這個值,int四個位元組,四個字解32位都是0值就是0,而char一個位元組8為都是0才會是0,那麼char和int的就能行了。但是即使是0很多人都會搞錯。
x是int型的,你要先把x的數值10轉換為字元串「10」,所以寫一個增加一個字元數組xx用來保存x的值轉換為的字元串,所以Writelnint要改。寫法如下()
voidWritelnInt(inti)
{
charxx[12];
sprintf(xx,"%d",i);
WriteData(xx,strlen(xx)+1);
}
同時,我WriteData也要改
voidWriteData(char*data,intlen)
{
char*newPos=m_pData+m_iWriteOffset;
memcpy(newPos,data,len);
m_iWriteOffset+=len-1;
}
因為傳遞過來的len的長度是要復制的字元的個數,這包含了一個結束標志,所以加的時候要減1,而且這要下一次連接之是就剛好是從結束標志所在的位置開始(或者說下一次運行是的newPos就是這個結束標志的位置)
完整的代碼在附件里
這是運行結果