Memset function, memset

Source: Internet
Author: User

Memset function, memset

1 // program example 2 # include <stdio. h> 3 # include <string. h> 4 5 int main (void) 6 {7 int I, j, k; 8 char buffer [] = "Helloworld \ n"; 9 printf ("Buffer before memset: % s ", buffer); // output result Buffer before memset: Helloworld10 memset (buffer, '*', strlen (buffer); 11 printf (" Buffer after memset: % s \ n ", buffer); // output result Buffer after memset: * ********** 12 13 // The ch in this section can be of int or other types, not necessarily char type 14 int array [5] =, 3, 5, 2}; 15 (I = 0; I <5; I ++) 16 printf ("% d", array [I]); // output result 1 4 3 5 217 printf ("\ n"); 18 19 memset (array, 0, 5 * sizeof (int); 20 for (j = 0; j <5; j ++) 21 printf ("% d", array [j]); // The output result is 0 0 0 0 022 printf ("\ n "); 23 24 memset (array, 1, 5 * sizeof (int); // use memset to assign values by byte, the value of each array element is actually 0x01010101, that is, decimal 1684300925 for (k = 0; k <5; k ++) 26 printf ("% d", array [k]); // output result 16843009 16843009 16843009 1684300927 printf ("\ n"); 28 return 0; 29} 30/* 31 * Because memset operates on bytes, in bytes, it assigns a value to the four bytes of memory pointed to by array. Each byte is filled with 1 in number, after being converted to binary, 1 is 0000, 32 * an int element is 4 bytes, starting from 0001, 01010101, 16843009, and. If it is converted to hexadecimal, It is 0 x, which is equal, assign a value of 33 * To an int element. Therefore, it is not advisable to use memset to assign an initial value to a non-struct array! 34 */

Reference link:

Http://baike.baidu.com/link? Url = svs6WnjQKV7Ugx3SZke6BvyVy99OOE8I-zn8gSw0HFb-YD-IIcdf2F2h5WGslM4Q4Dog28oXyjX51lnvF2n0Kq

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.