The String Header File

The string.h header

Requirements:
Linux Distribution
C Compiler
a shell interface
My Setup:
Debian GNU/Linux
GCC
BASH

We havent really touched much on the standard header files, so I will go
through some of them one by one. Today we will start off with the string.h
header file. The header file declares one type, which is size_t , as well
as the macro NULL. These are both defined in the stddef.h header as well, and
are described the same way. size_t is an unsigned integer type of the sizeof()
operator. NULL is a macro that expands to an “implementation-defined” null
pointer constant . The rest of string.h includes several functions I will be
going over.
The first four functions I will be discussing are memcpy, memmove, strcpy, and
strncpy:

#include <string.h>

void *memcpy(void * restrict s1, const void * restrict s2, size_t n);
void *memmove(void *s1, const void *s2, size_t n);
char *strcpy(char * restrict s1, const char * restrict s2);
char *strncpy(char * restrict s1, const char * restrict s2, size_t n);

The memcpy function copies characters from the object pointed to by s2 , into
the object pointed to by s1. The “n” argument is used to tell the function how
many characters to copy. However, if copying takes place between objects that
overlap, it results in undefined behavior. The memmove function on the other
hand, will treat copying by first storing the data in a temporary array of
“n” characters that does not overlap the objects pointed to by s1 and s2. The
strcpy function is similar to memcpy and will stop when it hits a null
character.The main difference is that strcpy is meant to deal with strings, and
memcpy could be any type of data. The strncpy is more similar to the memcpy as
well, since it requires you to specify the amount of bytes that are being copied
to the destination string. All memcpy, memmove, strcpy and strncpy return the
value of s1.

Now that we went over the 4 main copying functions, lets go over the string
concatenation functions:

#include <string.h>

char * strcat(char * restrict s1, const char * restrict s2);
char * strncat(char * restrict s1, const char *restrict s2, size_t n);

The strcat function appends a copy of the string pointed to by s2, including
the null terminating character, to the end of the string pointed to by s1.
The initial character of s2 overwrites the null character at the end of s1.
However, once again , if copying happens between objects that overlap, that
behavior is undefined. Similar to the strcat function, the strncat function does
the exact same thing, however you must specify how many bytes to write. This may
seem like a more safe way to use strcat, and could lead to less error in code.
A null terminating character will always be appended to s1. Both functions
return the value of s1.

The next category of functions are comparison functions:

#include <string.h>

int memcmp(const void *s1, const void *s2, size_t n);
int strcmp(const char *s1, const char *s2);
int strcoll(const char *s1, const char *s2);
int strncmp(const char *s1, const char* s2, size_t n);
size_t strxfrm(char * restrict s1, const char * restrict s2, size_t n);

The memcmp function compares the first n characters of the object pointed to
by s1 to the first n characters of the object pointed to by s2. Notice how most
of these functions pass in const data types. This is because the variables
passed in will not be manipulated during the function. The memcmp function
returns an integer greater than, equal to, or less than zero accordingly as
the object pointed to by s1 is greater than, equal to, or less than the object
pointed to by s2. Internally , this is most likely done by creating a temporary
integer, and if the first character of s1 is greater than the first character,
then increment the temporary integer, if its less then, decrement the integer,
or if its equal, do not touch that integer, and then after n bytes, return that
integer. The strcmp function acts the same, however it is meant to handle
strings rather then generic data. It will stop once the comparison hits a
null terminating character most likely. The strcoll function compares the string
pointed to by s1 to the string pointed to by s2, both interpretted as
appropriate to the LC_COLLATE category of the current locale. This is an
enviornment variable which is implementation defined , I personally have not
used this function before, however it is a part of string.h . The strncmp is
similar to the strcpy function, with the option of setting the amount of
characters to compare with the n parameter. The strxfrm function transforms
the string pointed to by s2 and places the resulting string into the array
pointed to by s1. This transformation is done as if the strcmp function was
applied to two transformed strings, and returns the length of the transformed
string not including the null character.

Next will be a series of Search functions :

#include <string.h>

void * memchr(const void *s, int c, size_t n);
char * strchr(const char *s, int c);
size_t strcspn(const char *s1, const char *s2);
char * strpbrk(const char *s1, const char *s2);
char * strrchr(const char *s, int c);
size_t strspn(const char *s1, const char *s2);
char * strstr(const char *s1, const char *s2);
char * strtok(char * restrict s1, const char * restrict s2);

The memchr function locates the first occurence of c , which is converted
into an unsigned char, in the initial n characters of the object, pointed to by
s. This then returns a pointer to the located character or null pointer if the
character does not occur. The strchr is similar, however it reads until a null
terminating character and it converts c into a char rather then unsigned char.
It still returns a pointer to the located character, or null if the character
does not occur. The strcspn function returns the length of the maximum initial
segment of the string pointed to by s1, which consists entirely of characters
not from the string pointed to by s2. The strpbrk function locates the first
occurence in the string pointed to by s1 of any character from the string
pointed to by s2 and returns a pointer to the character, or a null pointer if
no character from s2 occured in s1. The strrchar function works similar to the
strchr function, however, it returns the last occurence of c, which is converted
into an char , pointed to by s. If the character does not occur, a null pointer
is returned. The strspn works similar to strcspn, but focuses on characters
from the string pointed to s2 occurring in s1 rather then not occurring. The
strtok function is interesting. A sequence of calls to the strtok function
can break the string pointed to by s1 into a sequence of tokens. This function
returns a pointer to the first character of a token, or a null pointer if there
is no token. When you first call this function, you would pass a string into
s1. On later calls, you would call this function with NULL to continue
tokenizing that string.

The final set of functions are miscellanious functions :

#include <string.h>

void *memset(void *s, int c, size_t n);
char *strerror(int errnum);
size_t strlen(const char *s);

The memset function copies the value of c , which is converted into an
unsigned char, into each of the first n characters of the object specified and
then returns the value of s. The strerror function maps the number in errnum
to a message string. It returns contents that are locale-specific and shall
not be modified by the program, but may be overwritten by another call to
strerror. And lastly, the strlen function returns the length of the string
pointed to by s.

Leave a Reply