Strcmp/strncmp

From 탱이의 잡동사니
Jump to navigation Jump to search

Overview

strcmp/strncmp 내용 정리

Basic

<source lang=c>

  1. include <string.h>

int strcmp(const char *s1, const char *s2);

int strncmp(const char *s1, const char *s2, size_t n); </source>

strcmp()/strncmp() 사용시, 문자열 인자값에 NULL 이 입력되면 세그멘테이션 폴트를 일으킨다.

Example

<source lang=c>

  1. include <stdio.h>
  2. include <string.h>

int main(int argc, char** argv) {

   char* tmp;
   int ret;
   
   tmp = "hello";
   ret = strncmp(tmp, "helloooo", strlen(tmp));
   
   printf("Reuslt. ret[%d]\n", ret);
   
   return 0;

} </source>

Results

$ valgrind --tool=memcheck --leak-check=full ./main
==32434== Memcheck, a memory error detector
==32434== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==32434== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==32434== Command: ./main
==32434== 
Reuslt. ret[0]
==32434== 
==32434== HEAP SUMMARY:
==32434==     in use at exit: 0 bytes in 0 blocks
==32434==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==32434== 
==32434== All heap blocks were freed -- no leaks are possible
==32434== 
==32434== For counts of detected and suppressed errors, rerun with: -v
==32434== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)