[function] strdup()

duckkuri·2020년 10월 10일
0

libc_functions

목록 보기
22/22

[function] strdup()

라이브러리 : libc

헤더 : string.h

  • 복사한 문자열을 저장
  • strdub()함수는 문자열 s1을 복사하기 위한 충분한 메모리를 할당후 복사를 한 다음 그 포인터를 리턴한다.
  • 이후 free()함수의 인수로도 사용할 수있다.
  • 만약 사용가능한 메모리가 충분하지 않은 경우, NULL이 리턴됨 errno에 ENOMEM이 설정된다.

형식

strdup(const char *s1);

const char *s1 : 복사할 문자열 포인터

리턴값

char * 형 데이터로 리턴
  • s1 문자열을 복사한 저장된 메모리 포인터 리턴

사용 예제

char s1[]="hello world";
char *s2;
s2 = strdup(s1);
printf("s1 : %p, strdup : %p, %s\n", s1, s2, s2);	// s1 : 0x7ffee980d9ec, strdup : 0x7ff32dc00630, hello world

참고 링크
https://www.freebsd.org/cgi/man.cgi?query=index&sektion=3&apropos=0&manpath=freebsd

profile
😤 Today I Learned

0개의 댓글