Memset, memcpy, and atoi Functions

Source: Internet
Author: User

1. Void * memset (void * s, int C, size_t N)

Purpose: set the value of the first n Bytes of the memory space S to the value c.

2. Example

#includevoid main(){char *s="Golden Global View";clrscr();memset(s,‘G‘,6);printf("%s",s);getchar();return 0;}

3. The memset () function is often used for memory space initialization. For example:

char str[100];memset(str,0,100);

4. Deep connotation of memset (): it is used to set all memory space to a specific character. It is generally used to initialize the defined string to ''or '/0'. For example: char A [100]; memset (A, '/0', sizeof ());

Memcpy is used for memory copying.You can use it to copy any data type object and specify the length of the copied data. For example, char a [100], B [50]; memcpy (B,, sizeof (B); If sizeof (a) is used, the memory address of B may overflow.

Strcpy can only copy strings. It ends copying when '/0' is encountered. For example, char a [100], B [50]; strcpy (A, B ); if strcpy (B, A) is used, check whether the length of the string in A (before the first '/0') exceeds 50 bits. If it exceeds, this will cause memory address overflow of B.

5. Added: Some experiences
Memset can easily clear a variable or array of the structure type.
For example:

struct sample_struct{char csName[16];int iSeq;int iType;};

For Variables

struct sample_strcut stTest;

In general, the method of clearing sttest is as follows:

stTest.csName[0]=‘/0‘;stTest.iSeq=0;stTest.iType=0;

Memset is very convenient:

memset(&stTest,0,sizeof(struct sample_struct));

If it is an array:

struct sample_struct TEST[10];

Then

memset(TEST,0,sizeof(struct sample_struct)*10);

6. Strcpy

Prototype: extern char * strcpy (char * DEST, char * SRC );
Usage: # I nclude
Function: Copies the string ending with null indicated by Src to the array indicated by DeST.
Note: The memory areas specified by Src and DEST cannot overlap and DEST must have sufficient space to accommodate SRC strings.
Returns the pointer to DeST.
Memcpy
Prototype: extern void * memcpy (void * DEST, void * SRC, unsigned int count );
Usage: # I nclude
Function: copy count bytes from the memory area indicated by Src to the memory area indicated by DeST.
Note: the memory areas specified by Src and DEST cannot overlap. The function returns a pointer to DeST.
Memset
Prototype: extern void * memset (void * buffer, int C, int count );
Usage: # I nclude
Function: sets the first count byte of the memory area referred to by buffer to character C.
Note: The pointer to the buffer is returned.

Atoi () function

Prototype: int atoi (const char * nptr)

Usage: # include <stdlib. h>

Function: converts a string to an integer. atoi () scans the nptr parameter string and skips the leading space character until the conversion starts when a number or positive or negative sign is encountered, when a non-digit or string ('\ 0') is encountered, the conversion is terminated and the result is returned.

Description: The atoi () function returns the converted integer.

Memset, memcpy, and atoi Functions

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.