Linux common c Function Memory and string operations

Source: Internet
Author: User
Tags strtok

Bcmp (memory content comparison)

Related functions: bcmp, strcasecmp, strcmp, strcoll, strncmp, strncasecmp

Header file # include <string. h>

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 <string. h>

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 <string. h>

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 [I]);

Memcpy (dest src, 30);/* put the Dest pointer on money */

Printf ('\ nmemcpy ():');

For (I = 0; I <30; I ++)

Printf ("% C", Dest [I]);

Run bcopy (): String string

Memcpy (): String sring

  Bzero (clear all memory content to zero)

Related functions: memset and swab

Header file # include <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 a <string C, return-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 <string. h>

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 <string. h>

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", a [I]);

Memcpy (a, B, 30 );

Printf ("\ nmemcpy ():");

For (I = 0; I <30; I ++)

Printf ("% C", a [I]);

}

Execute strcpy (): string)

Memcpy (): String string

  Memmove (copy memory content)

Related functions: bcopy, memccpy, memcpy, strcpy, strncpy

Header file # include <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. H.>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <include. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 <string. h>

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 */

PS: makefile beginners encounter under the CFLAGS-----MARK

Setenv (set environment variable)

Function Description:Query or display environment variables.

Syntax:Setenv [variable name] [variable value]

Note:Setenv is the command for querying or setting environment variables in Tsch.

Setenv (change or add environment variables), related functions: getenv, putenv, unsetenv

Header file # include <stdlib. h>

Defines the function int setenv (const char * Name, const char * value, int overwrite );

Function Description: setenv () is used to change or add environment variables. The parameter name is an environment variable name string. The value parameter is the content of the variable. Overwrite is used to determine whether to change the existing environment variable. If the value of overwrite is not 0 and the environment variable already exists, the original content will be changed to the variable content referred to by the parameter value. If overwrite is 0 and the environment variable already contains content, the parameter value is ignored. If the return value is successfully executed, 0 is returned. If an error occurs,-1 is returned.

Error code: insufficient enomem memory, unable to configure new environment variable space

_____________________________________________________

PS: I haven't looked at Linux for a long time: after self-reflection, I continue to do this on

The exec function family finds an executable file based on the specified file name and uses it to replace the content of the calling process. In other words, it executes an executable file within the calling process. The executable file can be either a binary file or a script file that can be executed in any Linux environment.

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.