SUBJECT: Memory 관련 함수들
Description :
o memcpy()
=> copy characters between buffers (문자열 복사)
#include
char *memcpy(dest,source,cnt);
char *dest;
char *source;
in t cnt; --> 문자열의 수
=> memcpy()는 'source'에서 'dest'로 'cnt'만큼의 문자를 복사한다.
만일 'dest'와 'source'의 일부분이 겹치면 복사를 하는 중에 'source'의
내용을 잃어버릴 수 있기 때문에 memcpy()는 'source'를 겹치지 않도록
해야 한다.
<결과> 'dest'의 포인터
♨ 예제
#include
char *buffr1="haco";
char *buffr2="ura";
main()
{
memcpy(buffr1,buffr2,4);
printf("%s\n",buffr1);
}
hyundai2% a.out
ura
o memset()
=> Initialize buffer (버퍼를 초기화)
#include
char *memset(dest,ch,cnt);
char *dest;
int ch;
int cnt;
=> memset()은 버퍼 'dest'의 내용을 처음부터 'cnt'까지에 'ch'의 값을 써넣는다.
<결과> 'dest'의 포인터
♨ 예제
#include #include
char buff[6]="sikgi"; char buff[6]="sikgi";
main() main()
{ {
printf("%s...1\n", buff); printf("%s...1\n",buff);
memset(buff,'\k',6); memset(buff,'\0',6);
printf("%s...2\n", buff); printf("%s...2\n",buff);
} }
hyundai2% a.out hyundai2% a.out
sikgi...1 sikgi...1
kkkkkk...2 ...2
hyundai2% a.out > test1 hyundai2% a.out > test2
hyundai2% ljsdump test1
0000 : 73 69 6b 67 69 2e 2e 2e 31 0a 6b 6b 6b 6b 6b 6b sikgi...1.kkkkkk
0010 : 2e 2e 2e 32 0a ...2.
hyundai2% ljsdump test2
0000 : 73 69 6b 67 69 2e 2e 2e 31 0a 2e 2e 2e 32 0a sikgi...1....2.
o memcmp()
=> compare characters from two buffers (문자열 비교)
#include
int memcmp(str1,str2,cnt);
char *str1; ---> 첫번째 버퍼
char *str2; ---> 두번째 버퍼
int cnt; ---> 문자열의 수
=> memcmp()는 'cnt'만큼의 문자를 'str1'과 'str2'에서 비교한다.
(여기에서 'a'는 'b'보다 작고, 'b'는 'b'와 같고, 'c'는 'b'보다 크다.)
<결과> 두개 버퍼 사이의 관계를 가리키는 값
-------------------------------------
결 과 | 돌아오는 값
-------------------------------------
str1 < str2 | < 0
str1 == str2 | 0
str1 > str2 | > 0
-------------------------------------
<주의> 대소 문자는 구별된다. 즉, 'a'는 'A'와 같지 않다.
♨ 예제
#include
char str1[11]="skiyaki";
char str2[11]="nobadayaki";
int comp;
main()
{
comp=memcmp(str1,str2,10);
printf("%d\n", comp);
}
hyundai2% a.out
5 --> 's' - 'n' = 115 - 110
Revision History
작성일자 : 96.10.11
작성자 : 이진수
수정일자 :
수정자 :