On-board project Problem Solving

Source: Internet
Author: User

1 memset Function Solution

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 # include
Void 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 as ''or '/0'; 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, a, sizeof (B); note that 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. Supplement: memset can easily clear a variable or array of the structure type.
Example: struct sample_struct {char csname [16]; int iseq; int itype ;};
For the variable struct sample_strcut sttest;
Generally, sttest is cleared by sttest. csname [0] = '/0'; sttest. iseq = 0; sttest. itype = 0;
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: copy the string of SRC ending with null 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: set the first count bytes of the memory area referred to by buffer to the character C. Note: The pointer to the buffer is returned.

 

Reprinted

On-board project Problem Solving

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.