C Language Memset Learning

Source: Internet
Author: User

#include <stdio.h>#include<memory.h>voidShowinta[][3]);//function DeclarationvoidMain () {inta[][3]={{ at, $, One},{ About, $, the},{ the, -, +},{ One, A, -}};//definition of a two-dimensional arrayShow (a);//a two-dimensional array as a formal parameter, traversing the displayMemset (A,0,sizeof(a));//0 operation for two-dimensional array, i.e. clear 0Show (a); }voidShowinta[][3]){   inti,j;  for(i=0;i<4; i++){         for(j=0;j<3; j + +) {printf ("%d", A[i][j]); } printf ("\ n"); }}

Function: Sets the contents of each byte in a piece of memory pointed by S to the ASCII value specified by CH,

The size of the block is specified by the third parameter, which typically initializes the newly requested memory.

Usage: void *memset (void *s, char ch, unsigned n);

program Example:

#include <string.h>

#include <stdio.h>

#include <memory.h>

int main (void)

{

Char buffer[] = "Hello world/n";

printf ("Buffer before memset:%s/n", buffer);

memset (buffer, ' * ', strlen (buffer));

printf ("Buffer after memset:%s/n", buffer);

return 0;

}

Output Result:

Buffer before Memset:hello World

Buffer after Memset: ***********

In general, all modifications are made to the array, not to Cahr *s= "I am student" because

s points to a read-only memory, and you memset try to modify it, so the runtime to error, modify the method char *s modified to char s[]

C Language Memset Learning

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.