String functions, C string functions

Source: Internet
Author: User

String functions, C string functions

1. memset

# Include <stdio. h> # include <string. h> int main () {// char * s = "My Dream Come true"; // The String constant used to initialize the character pointer will be scheduled by the compiler to the read-only data storage area, is an unchangeable char s [] = "My Dream Come true"; // allocate space for memset (s, 'B', 6) in the stack ); // printf ("% s \ n", s) in bytes; // bbbbbbam Come true return 0 ;}

Memset can easily clear a variable or array of the structure type.
Example:
Variable: struct position pos;
Memset (& pos, 0, sizeof (struct position ));
Array:
Struct position pos [10];
Memset (pos, 0, sizeof (struct position) * 10 );

2. memcpy

Strcpy is usually used to copy strings, while memcpy is generally used to copy data of other types.

Memcpy can copy any content, such as character arrays, integers, structs, and classes.

// Memcpy is used for memory copying. You can use it to copy any data type object. You can specify the copied Data Length # include <stdio. h> # include <string. h> int main () {char a [27] = {'\ 0'}, B [30] = {' \ 0'}; for (int I = 0; I <26; I ++) a [I] = 'A' + I; memcpy (B, A, sizeof (B )); // copy the content of array a to array B. The length of array B should not be less than a puts (B); // ABCD ..... Z return 0 ;}

If the length of the above array B is less than a, B will first be assigned a long string such as B array, In addition, will append the string of the entire array

Related Article

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.