Common Linux C functions-memory and string operations

Source: Internet
Author: User
Tags strtok
Common Linux C functions-memory and string operations-general Linux technology-Linux programming and kernel information. For more information, see the following. Bcmp (memory content comparison)
Related functions: bcmp, strcasecmp, strcmp, strcoll, strncmp, strncasecmp

Header file # include

Define the function int bcmp (const void * s1, const void * s2, int n );

Function Description: bcmp () is used to compare the first n bytes in the memory interval specified by s1 and s2. If the parameter n is 0, 0 is returned.

If the memory content of parameters s1 and s2 are identical, the return value is 0. Otherwise, the return value is non-zero.

We recommend that you replace memcmp () with additional instructions.

For an example, refer to memcmp ().

 




Bcopy (copy memory content)
Related functions: memccpy, memcpy, memmove, strcpy, ctrncpy

Header file # include

Define the void bcopy function (const void * src, void * dest, int n );

Function Description: bcopy () and memcpy () are used to copy the first n Bytes of memory content specified by src to the address specified by dest, however, the src parameter and the dest parameter are in the opposite position when being passed to the function.

Return Value

We recommend that you replace memcpy () with additional instructions.

Example # include
Main ()
{
Char dest [30] = "string ()";
Char src [30] = "string \ 0string ";
Int I;
Bcopy (src, dest, 30);/* place the src pointer before */
Printf (bcopy (): ")
For (I = 0; I <30; I ++)
Printf ("% c", dest );
Memcpy (dest src, 30);/* put the dest pointer on money */
Printf ('\ nmemcpy ():');
For (I = 0; I <30; I ++)
Printf ("% c", dest );

Run bcopy (): string
Memcpy (): string sring

 




Bzero (clear all memory content to zero)
Related functions: memset and swab

Header file # include

Define the void bzero (void * s, int n) function );

Function Description: bzero () sets the first n Bytes of the memory area specified by parameter s to zero. It is equivalent to calling memset (void *) s, 0, size_tn );

Return Value

Memset is recommended for additional instructions.

For an example, refer to memset ().

 




Index (find the first specified character in the string)
Related functions: rindex, srechr, strrchr

Header file # include

Define the function char * index (const char * s, int c );

Function Description index () is used to find the first c Address in the parameter s string, and then return the address of the character. The string ending character (NULL) is also considered as part of the string.

Return value if a specified character is found, the address of the character is returned; otherwise, 0 is returned.

Example # include
Main ()
{
Char * s = "0123456789012345678901234567890 ";
Char * p;
P = index (s, '5 ');
Printf (% s \ n ", p );
}

Run 5.68E + 25

 




Memccpy (copy memory content)
Related functions: bcopy, memcpy, memmove, strcpy, strncpy

Header file # include

Define the function void * memccpy (void * dest, const void * src, int c, size_t n );

Function Description: memccpy () is used to copy the first n Bytes of memory content in src to the address indicated by dest. Unlike memcpy (), memccpy () checks whether parameter c appears during replication. If so, the value of dest is the next byte address of c.

The Return Value Returns the next byte pointer pointing to the value of c in dest. If the returned value is 0, the first n bytes in the memory indicated by src do not contain the bytes whose value is c.

Example # include
Main ()
{
Char a [] = "string [a]";
Char B [] = "string (B )";
Memccpy (a, B, 'B', sizeof (B ));
Printf ("memccpy (): % s \ n", );
}

Execute memccpy (): string (B)

 




Memchr (find a specific character in a memory range)
Related functions: index, rindex, strchr, strpbrk, strrchr, strsep, strspn, strstr

Header file # include

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

Function Description: memchr () searches for the first n Bytes of memory content specified by s from the beginning until the first byte with the value of c is found, the pointer pointing to the byte is returned.

If the specified byte is found, the pointer of the byte is returned. Otherwise, 0 is returned.

Example # include
Main ()
{
Char xs = "0123456789012345678901234567890 ";
Char * p;
P = memchr (s, '5', 10 );
Printf ("% s \ n", p );
}

Run 5.68E + 25

 




Memcmp (memory content comparison)
Related functions: bcmp, strcasecmp, strcmp, strcoll, strncmp, strncasecmp

Header file # include

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

Function Description: memcmp () is used to compare the first n characters in the memory interval specified by s1 and s2. The comparison of string size is determined by the order in the ASCII code table, and the sub-order is also the character value. Memcmp () First subtract the value of the first character of s1 from the value of the first character of s2. If the difference is 0, the next character is compared. If the difference is not 0, the difference is returned. For example, if the string "Ac" and "ba" is compared, the difference (-33) between the characters 'A' (65) and 'B' (98) is returned ).

Return Value: if the memory content specified by parameters s1 and s2 are identical, the return value is 0. If s1 is greater than s2, a value greater than 0 is returned. If s1 is smaller than s2, a value smaller than 0 is returned.

Example # include
Main ()
{
Char * a = "aBcDeF ";
Char * B = "AbCdEf ";
Char * c = "aacdef ";
Char * d = "aBcDeF ";
Printf ("memcmp (a, B): % d \ n", memcmp (void *) a, (void *) B, 6 ));
Printf ("memcmp (a, c): % d \ n", memcmp (void *) a, (void *) c, 6 ));
Printf ("memcmp (a, d): % d \ n", memcmp (void *) a, (void *) d, 6 ));

Execute memcmp (a, B): 1/* string a> string B, returns 1 */
Memcmp (a, c):-1/* string <字符串c,返回-1*
Memcmp (a, d): 0/* string a = string d, return 0 */

 




Memcpy (copy memory content)
Related functions: bcopy, memccpy, memcpy, memmove, strcpy, strncpy

Header file # include

Define the function void * memcpy (void * dest, const void * src, size_t n );

Function Description: memcpy () is used to copy the first n Bytes of memory content in src to the memory address in dest. Unlike strcpy (), memcpy () completely copies n Bytes and does not end with the string ending '\ 0.

Returns the pointer to the dest value.

Note that the memory areas indicated by the src and dest pointers cannot overlap.

Example # include
Main ()
{
Char a [30] = "string ()";
Char B [30] = "string \ 0 string ";
Int I;
Strcpy (a, B );
Printf ("strcpy ():");
For (I = 0; I <30; I ++)
Printf ("% c", );
Memcpy (a, B, 30 );
Printf ("\ nmemcpy ():");
For (I = 0; I <30; I ++)
Printf ("% c", );
}

Execute strcpy (): string)
Memcpy (): string

 




Memmove (copy memory content)
Related functions: bcopy, memccpy, memcpy, strcpy, strncpy

Header file # include

Define the function void * memmove (void * dest, const void * src, size_t n );

The function indicates that memmove () and memcpy () are used to copy the first n Bytes of memory content in src to the address indicated by dest. The difference is that when src and dest refer to memory areas overlapping, memmove () can still be correctly processed, but the execution efficiency is slightly slower than memcpy.

Returns the pointer to the dest value.

Additional instructions: the memory areas indicated by the src and dest pointers can overlap.

For an example, refer to memcpy ().

 




Memset (enter a certain value for a piece of memory space)
Related functions bzero, swab

Header file # include

Define the function void * memset (void * s, int c, size_t n );

Function Description: memset () will fill in the first n Bytes of the memory area referred to by parameter s with parameter c, and then return the pointer to s. When writing a program, if you need to initialize an array, memset () is quite convenient.

Returns the pointer to s.

Note that although the parameter c is declared as int, it must be an unsigned char, so the range is between 0 and 255.

Example # include
Main ()
{
Char s [30];
Memset (s, 'A', sizeof (s ));
S [30] = '\ 0 ';
Printf ("% s \ n", s );
}

Run aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

 




Rindex (find the last occurrence of the specified character in the string)
Related functions: index, memchr, strchr, strrchr

Header file # include

Define the function char * rindex (const char * s, int c );

Function Description: rindex () is used to locate the last occurrence of the parameter c address in the parameter s string, and then return the address of the character. The string ending character (NULL) is also considered as part of the string.

Return value if a specified character is found, the address of the character is returned. Otherwise, 0 is returned.

Example # include
Mian ()
{
Char xs = "0123456789012345678901234567890 ";
Char * p;
P = rindex (s, '5 ');
Printf ("% s \ n", p );
}

Run 567890

 




Strcasecmp (case-insensitive strings are ignored)
Related functions: bcmp, memcmp, strcmp, strcoll, strncmp

Header file # include

Define the int strcasecmp (const char * s1, const char * s2) function );

Function Description: strcasecmp () is used to compare s1 and s2 strings. Differences in Case sensitivity are automatically ignored during comparison.

Returns 0 if the s1 and s2 parameters are the same. If the s1 length is greater than the s2 length, a value greater than 0 is returned. If the s1 length is smaller than the s2 length, a value smaller than 0 is returned.

Example # include
Main ()
{
Char * a = "aBcDeF ";
Char * B = "AbCdEf ";
If (! Strcasecmp (a, B ))
Printf ("% s = % s \ n", a, B );
}

Run aBcDeF = AbCdEf

 




Strcat (connect two strings)
Related functions: bcopy, memccpy, memcpy, strcpy, strncpy

Header file # include

Define the function char * strcat (char * dest, const char * src );

Function Description strcat () copies the src string to the end of the string referred to by the dest parameter. The first dest parameter requires sufficient space to accommodate the string to be copied.

Returns the start address of the string of the dest parameter.

Example # include
Main ()
{
Char a [30] = "string (1 )";
Char B [] = "string (2 )";
Printf ("before strcat (): % s \ n", );
Printf ("after strcat (): % s \ n", strcat (a, B ));
}

Execute before strcat (): string (1)
After strcat (): string (1) string (2)

 




Strchr (find the first occurrence of the specified character in the string)
Related functions: index, memchr, rinex, strbrk, strsep, strspn, strstr, strtok

Header file # include

Define the function char * strchr (const char * s, int c );

Function Description strchr () is used to find the first c Address in the parameter s string, and then return the address of the character.

Return value if a specified character is found, the address of the character is returned; otherwise, 0 is returned.

Example # include
Main ()
{
Char * s = 0123456789012345678901234567890 ";
Char * p;
P = strchr (s, '5 ');
Printf ("% s \ n", p );
}

Run 5.68E + 25

 




Strcmp (comparison string)
Related functions: bcmp, memcmp, strcasecmp, strncasecmp, strcoll

Header file # include

Defines the int strcmp (const char * s1, const char * s2) function );

Function Description strcmp () is used to compare parameters s1 and s2 strings. The string size is determined by the order in the ASCII code table, which is also the character value. Strcmp () First subtract the first character value of s1 from the first character value of s2. If the difference value is 0, the next character is compared. If the difference value is not 0, the difference is returned. For example, if the string "Ac" and "ba" is compared, the difference (-33) between the character "A" (65) and 'B' (98) is returned ).

Returns 0 if the s1 and s2 parameters are the same. If s1 is greater than s2, a value greater than 0 is returned. If s1 is smaller than s2, a value smaller than 0 is returned.

Example # include
Main ()
{
Char * a = "aBcDeF ";
Char * B = "AbCdEf ";
Char * c = "aacdef ";
Char * d = "aBcDeF ";
Printf ("strcmp (a, B): % d \ n", strcmp (a, B ));
Printf ("strcmp (a, c): % d \ n", strcmp (a, c ));
Printf ("strcmp (a, d): % d \ n", strcmp (a, d ));
}

Execute strcmp (a, B): 32
Strcmp (a, c):-31
Strcmp (a, d): 0

 




Strcoll (use the character arrangement order of the current region to compare strings)
Related functions: strcmp, bcmp, memcmp, strcasecmp, strncasecmp

Header file # include

Define the int strcoll (const char * s1, const char * s2) function );

Function Description: strcoll () compares s1 and s2 strings according to the text order specified by the Environment Variable LC_COLLATE.

Returns 0 if the s1 and s2 parameters are the same. If s1 is greater than s2, a value greater than 0 is returned. If s1 is smaller than s2, a value smaller than 0 is returned.

Note that if LC_COLLATE is POSIX or C, strcoll () and strcmp () Act exactly the same.

For an example, refer to strcmp ().

 




Strcpy (copy string)
Related functions: bcopy, memcpy, memccpy, and memmove

Header file # include

Define the function char * strcpy (char * dest, const char * src );

Function Description strcpy () copies the src string to the address indicated by the dest parameter.

Returns the start address of the string of the dest parameter.

Additional instructions: if the memory space specified by the dest parameter is not large enough, it may cause buffer Overflow errors. Please pay special attention when writing programs, or use strncpy ().

Example # include
Main ()
{
Char a [30] = "string (1 )";
Char B [] = "string (2 )";
Printf ("before strcpy (): % s \ n", );
Printf ("after strcpy (): % s \ n", strcpy (a, B ));
}

Execute before strcpy (): string (1)
After strcpy (): string (2)

 




Strcspn (returns the number of consecutive characters in the string that do not contain the content of the specified string)
Related functions

Header file # received

Define the function size_t strcspn (const char * s, const char * reject );

Function Description: strcspns () Calculate consecutive characters from the beginning of the parameter s string, and these characters are not included in the string referred to by the parameter reject. To put it simply, if the value returned by strcspn () is n, it means that n consecutive characters starting with string s do not contain any character in string reject.

The Return Value Returns the number of consecutive characters starting with string s that do not contain the string reject.

Example # include
Main ()
{
Char * str = "Linux was first developed for 386/486-based pcs .";
Printf ("% d \ n", strcspn (str ,""));
Printf ("% d \ n", strcspn (str ,"/-"));
Printf ("% d \ n", strcspn (str, "1234567890 "));
}

Execute 5/* only calculate the "", so the return length of "Linux */
33/* calculate the length of "/" or "-", so the return value is the length of "6 */
30/* The length before occurrence of the occurrence of a number character is returned */

 




Strdup (copy string)
Related functions: calloc, malloc, realloc, and free

Header file # include

Define the function char * strdup (const char * s );

Function Description strdup () will first configure the same space size as the parameter s string with maolloc (), then copy the content of the parameter s string to the memory address, and then return the address. You can use free () to release the address.

Returns a string pointer to the copied string address. If the return value is NULL, the memory is insufficient.

Example # include
Main ()
{
Char a [] = "strdup ";
Char * B;
B = strdup ();
Printf ("B [] = \" % s \ "\ n", B );
}

Run B [] = "strdup"

 




Strlen (returns the string length)
Related functions

Header file # include

Define the size_t strlen (const char * s) function );

Function Description strlen () is used to calculate the length of the specified string s, excluding the ending character "\ 0 ".

Returns the number of characters in string s.

Example/* Get the str length */
# Include
Main ()
{
Char * str = "12345678 ";
Printf ("str length = % d \ n", strlen (str ));
}

Run str length = 8

 




Strncasecmp (case-insensitive comparison string ignored)
Related functions: bcmp, memcmp, strcmp, strcoll, strncmp

Header file # include

Define the int strncasecmp function (const char * s1, const char * s2, size_t n );

Function Description: strncasecmp () is used to compare the first n characters of the s1 and s2 strings. The case differences are automatically ignored during the comparison.

Returns 0 if the s1 and s2 parameters are the same. If s1 is greater than s2, a value greater than 0 is returned. If s1 is smaller than s2, a value smaller than 0 is returned.

Example # include
Main ()
{
Char * a = "aBcDeF ";
Char * B = "AbCdEf ";
If (! Strncasecmp (a, B ))
Printf ("% s = % s \ n", a, B );
}

Run aBcDef = AbCdEf

 




Strncat (connect two strings)
Related functions: bcopy, memccpy, memecpy, strcpy, strncpy

Header file # inclue

Define the function char * strncat (char * dest, const char * src, size_t n );

Function Description strncat () copies the src string n characters to the end of the string referred to by the dest parameter. The first dest parameter requires sufficient space to accommodate the string to be copied.

Returns the start address of the string of the dest parameter.

Example # include
Main ()
{
Char a [30] = "string (1 )";
Char B [] = "string (2 )";
Printf ("before strnact (): % s \ n", );
Printf ("after strncat (): % s \ n", strncat (a, B, 6 ));
}

Execute before strnact (): string (1)
After strncat (): string (1) string

 




Strncpy (copy string)
Related functions: bcopy, memccpy, memcpy, and memmove

Header file # include

Define the function char * strncpy (char * dest, const char * src, size_t n );

Function Description strncpy () copies the src string to the address specified by dest.

Returns the start address of the string of the dest parameter.

Example # inclue
Main ()
{
Char a [30] = "string (1 )";
Char B [] = "string (2 )";
Printf ("before strncpy (): % s \ n", );
Printf ("after strncpy (): % s \ n", strncpy (a, B, 6 ));
}

Run before strncpy (): string (1)
After strncpy (): string (1)

 




Strpbrk (find the first occurrence of the specified character in the string)
Related functions: index, memchr, rindex, strpbrk, strsep, strspn, strstr, strtok

Header file # include

Define the function char * strpbrk (const char * s, const char * accept );

Function Description: strpbrk () is used to identify the first occurrence of any character in the accept string of the parameter s string.

Return value if a specified character is found, the address of the character is returned; otherwise, 0 is returned.

Example # include
Main ()
{
Char xs = "0123456789012345678901234567890 ";
Char * p;
P = strpbrk (s, "a1 839");/* 1 is first found in the s string */
Printf ("% s \ n", p );
P = strprk (s, "4398");/* 3 is first found in the s string */
Printf ("% s \ n", p );

Run 1.23E + 29.

 




Strrchr (find the last character in the string)
Related functions: index, memchr, rindex, strpbrk, strsep, strspn, strstr, strtok

Header file # include

Define the function char * strrchr (const char * s, int c );

Function Description strrchr () is used to find the last c address in the parameter s string, and then return the address of the character.

Return value if a specified character is found, the address of the character is returned; otherwise, 0 is returned.

Example # include
Main ()
{
Char xs = "0123456789012345678901234567890 ";
Char * p;
P = strrchr (s, '5 ');
Printf ("% s \ n", p );
}

Run 567890

 




Strspns (returns the number of consecutive characters in the string that do not contain the specified string)
Related functions: strcspns, strchr, strpbrk, strsep, strstr

Header file # include

Define the function size_t strspns (const char * s, const char * accept );

Function Description: strspns () Calculate consecutive characters from the beginning of the s string, and these characters are all characters in the string referred to by accept. To put it simply, if the value returned by strspn () is n, it means that n consecutive characters starting with string s belong to the character in string accept.

The Return Value Returns the number of consecutive characters starting with string s that contain the string accept.

Example # include
Main ()
{
Char * str = "Linux was first developed for 386/486-based PCs .";
Char * t1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
Printf ("% d \ n", strspn (str, t1 ));
}

Execute 5/* to calculate the upper and lower case letters. Does not contain "", so the length of Linux is returned. */

 




Strstr (search for the specified string in a string)
Related functions: index, memchr, rindex, strchr, strpbrk, strsep, strspn, strtok

Header file # include

Define the function char * strstr (const char * haystack, const char * needle );

Function Description strstr () searches for the string needle from the string haystack and returns the address that appears for the first time.

Returns the address of the first occurrence of the specified string. Otherwise, 0 is returned.

Example # include
Main ()
{
Char xs = "012345678901234567890123456789 ";
Char * p;
P = strstr (s, "901 ");
Printf ("% s \ n", p );
}

Run 9.01E + 21.

 




Strtok (split string)
Related functions: index, memchr, rindex, strpbrk, strsep, strspn, strstr

Header file # include

Define the function char * strtok (char * s, const char * delim );

Function Description strtok () is used to split strings into segments. The parameter s points to the string to be split, and the parameter delim is the split string. When strtok () when the delim Delimiter is found in the string of parameter s, the character is changed to \ 0. In the first call, strtok () must be given the parameter s string. In future calls, the parameter s is set to NULL. If each call is successful, the next split string pointer is returned.

The Return Value Returns the string pointer after the next split. If no split is available, NULL is returned.

Example # include
Main ()
{
Char s [] = "AB-cd: ef; gh: I-jkl; mnop; qrs-tu: vwx-y; z ";
Char * delim = "-:";
Char * p;
Printf ("% s"; strtok (s, delim ));
While (p = strtok (NULL, delim) printf ("% s", p );
Printf ("\ n ");
}

Execute AB cd ef; gh I jkl; mnop; qrs tu vwx y; z/*-And: the characters have been replaced by \ 0 characters */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.