Profile

ys2310

Author:ys2310
2008年春にNew York Cityにあるふる〜い大学を卒業。


Categories


new postings


new comments


new trackbacks


monthly archeive


FC2ブログ 転職
DATE: CATEGORY:スポンサー広告
上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。
| BLOG TOP |
DATE: CATEGORY:C/C++ string.h
Q: What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?

The strcpy() function is designed to work exclusively with strings. It copies each byte of the source string to the destination string and stops when the terminating null character ('\0') has been reached. On the other hand, the memcpy() function is designed to work with any type of data. Because not all data ends with a null character, you must provide the memcpy() function with the number of bytes you want to copy from the source to the destination.

[Quote}:http://www.geekinterview.com/question_details/3389

| BLOG TOP |
DATE: CATEGORY:C/C++ string.h
文字格納関数
memset
ヘッダ #include "string.h"
形 式 void *memset(void *s, int c, size_t n);
機 能 sが指すオブジェクトの先頭n文字に、unsigned char型に変換したcの値を代入する。
返却値 sの値を返す。

■実装例■
void *memset(void *s, int c, size_t n)
{
const unsigned char uc = c;
unsigned char *p = (unsigned char *)s;

while (n-- > 0)
*p++ = uc;

return (s);
}

[Quote]:http://bohyoh.com

| BLOG TOP |
DATE: CATEGORY:C/C++ string.h
文字列複写関数
memmove
ヘッダ #include "string.h"
形 式 void *memmove(void *s1, const void *s2, size_t n);
機 能 s2が指すオブジェクトの先頭n文字をs1が指すオブジェクトにコピーする。コピー元とコピー先が重なる場合も正しくコピーする。
返却値 s1の値を返す。

■実装例■
void *memmove(void *s1, const void *s2, size_t n)
{
char *p1 = (char *)s1;
const char *p2 = (const char *)s2;

if (p1 < p2 && p1 < p2 + n)
for (p1 += n, p2 += n; n > 0; n--) /* 後ろからコピー */
*p1-- = *p2--;
else
for ( ; n > 0; n--) /* 前からコピー */
*p1++ = *p2++;

return (s1);
}

■実装例■
#include

void *memmove(void *s1, const void *s2, size_t n)
{
register char *ss1 = s1;
register const char *ss2 = s2;
if (n != 0)
{
if (ss1 < ss2)
{
register const char *t = ss2 + n;
do
*ss1++ = *ss2++;
while (ss2 != t);
}
else if (ss1 > ss2)
{
register const char *t = ss2;
ss1 += n;
ss2 += n;
do
*--ss1 = *--ss2;
while (ss2 != t);
}
}
return s1;
}

[Quote]:http://bohyoh.com

| BLOG TOP |
DATE: CATEGORY:C/C++ string.h
文字列複写関数
memcpy
ヘッダ #include "string.h"
形 式 void *memcpy(void *s1, const void *s2, size_t n);
機 能 s2が指すオブジェクトからn文字を、s1が指すオブジェクトにコピーする。コピー元とコピー先が重なる場合の動作は未定義とする。
返却値 s1の値を返す。

■実装例■
void *memcpy(void *s1, const void *s2, size_t n)
{
char *p1 = (char *)s1;
const char *p2 = (const char *)s2;

while (n-- > 0) {
*p1 = *p2;
p1++;
p2++;
}
return (s1);
}

[Quote]:http://bohyoh.com

| BLOG TOP |
DATE: CATEGORY:C/C++ string.h
比較関数
memcmp
ヘッダ #include "string.h"
形 式 int memcmp(const void *s1, const void *s2, size_t n);
機 能 s1が指すオブジェクトの先頭n文字と、s2が指すオブジェクトの先頭n文字をunsigned char型の値として先頭から順に比較する。
返却値 等しければ0、s1がs2より大きければ正の整数値、s1がs2より小さければ負の整数値を返す。

■実装例■
int memcmp(const void *s1, const void *s2, size_t n)
{
const unsigned char *p1 = (const unsigned char *)s1;
const unsigned char *p2 = (const unsigned char *)s2;

while (n-- > 0) {
if (*p1 != *p2)
return (*p1 - *p2);
p1++;
p2++;
}
return (0);
}

[Quote]:http://bohyoh.com

| BLOG TOP |

copyright © Manhattan life all rights reserved.Powered by FC2ブログ