[- Disclaimer -]
아래 내용은 정보보안 공부 목적으로 작성된 것이나, 이를 토대로 허가되지 않은 대상에 실습을 진행할 경우 해킹 시도로 간주하여 법적 처벌을 받을 수 있음을 알려 드립니다.
strncpy()
✦ <string.h>
✦ NULL String을 만날 때까지 지정 문자 수만큼 복사
✦ Parameter1에 전달되는 Argument1
✧ Destination 문자열 주소
✦ Parameter2에 전달되는 Argument2
✧ Source 문자열 주소 전달
✦ Parameter3에 전달되는 Argument3
✧ 문자 수
✦ Return 값
✧ 성공
→ Destination 문자열
✧ 실패
→ 0
char *strncpy(char *strDest, const char *strSource, size_t count);
Plain Text
복사
Ex)
#include <stdio.h>
#include <string.h>
int main(void) {
char str[8] = {'\0'};
char* hello = "hello world";
strcpy(str, hello);
printf("%s", str);
return 0;
}
Plain Text
복사
hello
Plain Text
복사
#include <stdio.h>
#include <string.h>
int main(void) {
char str[8] = {'\0'};
char *hello = "hello world";
strncpy(str, hello, 7);
printf("%s", str);
return 0;
}
Plain Text
복사
hello w
Plain Text
복사
strncpy() 기능 구현 - Ex)
#include <stdio.h>
char *self_strncpy(char *destination, const char *source, size_t num);
int main(void) {
char str[8] = {'a'};
char *hello = "hello world";
self_strncpy(str, hello, 11);
str[7] = '\0';
printf("%s", str);
return 0;
}
char *self_strncpy(char *destination, const char *source, size_t num) {
size_t i = 0;
for(i = 0; i < num-1; i++) {
destination[i] = source[i];
if(source[i] == '\0')
break;
}
return destination;
}
Plain Text
복사
hello w
Plain Text
복사



