[C] About Library <string.h>

숭글·2022년 7월 15일
0

#include <string.h>

  • strlen
  • memset
  • memcpy
  • memmove
  • strlcpy
  • strlcat
  • strchr
  • strrchr
  • strncmp
  • memchr
  • strnstr
  • strdup

size_t strlen(const char *s);

computes the length of the string s.
return values
: the number of characters that precede the terminating NULL character. (so that means the length of string.)

void *memset(void *b, int c, size_t len);

writes len bytes of value c (converted to an unsigned char) to the string b.

  • It's mostly used when we initialize the array's element.

return values
: its first argument. (=address of b)

#include <strings.h>
void bzero(void *s, size_t n);

writes n zeroed bytes to the string s. If n is zero, bzero() does nothing.

  • It's encouraged to use memset(s, 0, n);

void *memcpy(void *dst, const void *src, size_t n);

copies n bytes from memory area src to memory area dst.
If dst and src overlap, behavior is undefined.

  • It's almost same as strcmp but it's copied in bytes.

return values
: the original value of dst. (=address of dst)

void *memmove(void *dst, const void *src, size_t len);

copies len bytes from string src to string dst.
The two strings may overlap. but it guarantee the well copying to dst.

  • I needed to copy src first to other address first before copying to dst. but it seems pretty inefficient so I made two ways copying depends on order of addresses of them.

return values
:the original value of dst. (=address of dst)

size_t strlcpy(char * dst, const char * src, size_t dstsize);

copies up to dstsize - 1 characters from the string src to dst, NUL-terminating the result if dstsize is not 0.
If the src and dst strings overlap, the behavior is undefined.
It's designed to be safer, more consistent, and less error prone replacements for the easily misused function strncpy.

return values
: the total length of the string they tried to create.
For strlcpy() that means the length of src.
If the return value is >= dstsize, the output string has been truncated.
It is the caller's responsibility to handle this.

  • It guarantees the NULL-terminating string. so maybe caller need to think twice about the parameter len. it returns the length of the copied string. so caller can assume whether it worked well or not and decide the next moves.

size_t strlcat(char *dst, const char *src, size_t dstsize);

concatenate strings. It's designed to be safer, more consistent, and less error prone replacements for the easily misused function strncat. strlcat() appends string src to the end of dst.
It will append at most dstsize - strlen(dst) - 1 characters. It will then NUL-terminate, unless *dstsize is 0 or the original dst string was longer than dstsize.
If the src and dst strings overlap, the behavior is undefined.

return values
: the total length of the string they tried to create.
For strlcat() that means the initial length of dst plus the length of src.

If the return value is >= dstsize, the output string has been truncated. It is the caller's responsibility to handle this.

  • its return value look complicated. just think this function returns the enough length that NULL should be placed.

char *strchr(const char *s, int c);

locates the first occurrence of c (converted to a char) in the string pointed to by s. The terminating null character is considered to be part of the string; therefore if c is `\0', the functions locate the terminating `\0'.

return values
: a pointer to the located character, or NULL if the character does not appear in the string.

char *strrchr(const char *s, int c);

function is identical to strchr(), except it locates the last occurrence of c.

return values
: a pointer to the located character, or NULL if the character does not appear in the string.

int strncmp(const char *s1, const char *s2, size_t n);

lexicographically compare the null-terminated strings s1 and s2.
strncmp() is designed for comparing strings rather than binary data, characters that appear after a `\0' character are not compared.

return values
: an integer greater than, equal to, or less than 0, according as the string s1 is greater than, equal to, or less than the string s2.
The comparison is done using unsigned characters, so that `\200' is greater than `\0'.

  • `\200' is greater than `\0'. <- means that we need to consider the unsigned char as well.

void *memchr(const void *s, int c, size_t n);

locates the first occurrence of c (converted to an unsigned char) in string s.

return values
: a pointer to the byte located, or NULL if no such byte exists within n bytes.

  • it looks same with the strchr. but strchr is more fit in string.
    memchr proceed the comparing in byte.

int memcmp(const void *s1, const void *s2, size_t n);

compares byte string s1 against byte string s2. Both strings are assumed to be n bytes long.

return values
: zero - if the two strings are identical.
otherwise - the difference between the first two differing bytes(treated as unsigned char values, so that `\200' is greater than `\0', for example).
Zero-length strings are always identical.

char *strnstr(const char *haystack, const char *needle, size_t len);

locates the first occurrence of the null-terminated string needle in the string haystack, where not more than len characters are searched.
Characters that appear after a `\0' character are not searched.
Since the strnstr() function is a FreeBSD specific API, it should only be used when portability is not a concern.

return values
: original haystack - If needle is an empty string.
NULL - If needle occurs nowhere in haystack
otherwise - a pointer to the first character of the first occurrence of needle is returned.

char *strdup(const char *s1);

allocates sufficient memory for a copy of the string s1, does the copy, and returns a pointer to it. The pointer may subsequently be used as an argument to the function free.

return values
: allocated array's address
, If insufficient memory is available, NULL is returned and errno is set to ENOMEM.

 
profile
Hi!😁 I'm Soongle. Welcome to my Velog!!!

0개의 댓글