memset Is a function that is often used to initialize arrays , Its definition is as follows :

void * memset ( void * ptr, int value, size_t num );

Its effect is to take ptr Is the starting address , The length is num In a memory range of bytes , The value of each byte is set to value.

There is a strange phenomenon here , Why? value The type of is declared to be int, But only the lowest byte is used ? Or why memset Only one byte value is needed to fill in , But it requires the user to pass in a int?

This has to come from C89 Speaking of standards , It is said that in C89 Before the standard comes out ,C The declaration of the function prototype is not mandatory in the code of , If a function call comes before its declaration , The compiler assumes a statement . for instance :

void bar() {
   int a = foo(5);
int foo( int x) {
   return x + 1;

In this code ,foo stay bar In the called , But the statement came after it , Today's compilers give compiler errors , But in C89 Before , The compiler depends on the statement of the function call ,int a = foo(5) Let's guess foo Function prototype of , For example, the value passed in is 5, It's just one. int, The return value is also a int.

So , In some old code ,memset Can occur before it is declared . But in C89 after , Function declaration becomes required , therefore memset It has to be declared first , At this point, in order to take care of the existing code , Such as

void * memset ( array, 0, sizeof (array) );

Even for char Array assignment memset, Such as

void * memset ( array, 'a' , sizeof (array) );

character constants ( Such as ’a') stay C Language is also considered to be int type , therefore memset You can only use the prototype of int 了 ……

That is to say , It's accepting ‘a’ Type of character , The actual type is int


Why? memset The second parameter of does not put int Replace with char More articles about

  1. About strings replace Methods explore the second parameter

    Online related replace There are a lot of articles , Let's talk about its second parameter . Reading this article needs to be right replace There is a certain understanding of the method .W3school=>replace We need to replace some specified characters in a string , first time ...

  2. About replace() The escape of the second parameter in the method

    If you want to pass Javascript The code is rendered in the web page \ character , It's in JS You have to enter two backslashes in the code \\, Otherwise, an error will be reported . such as : var a = "\"; alert(a); //chro ...

  3. String regular substitution replace The second parameter is the function

    according to JS In other words , as follows replace() The second argument to a method can also be a function . There is only one match in ( The string that matches the pattern ) Under the circumstances , Will pass... To this function 3 Parameters : Pattern matches . The position of the pattern match in the string and the original ...

  4. Study Message(5): About TApplicationEvents.OnMessage Second parameter of Can shield TWebBrowser Right-click menu : TApplicationEvents.OnMessage Second parameter of Handl ...

  5. parseInt The second parameter is explained in detail

    I was in a while ago stackOverflow I see two such problems on the Internet : Why? parseInt(8,3) == NaN,parseInt(16,3) == 1? Why? parseInt('dsff66',16) = ...

  6. IE6/7/8 in parseInt When the first parameter is an illegal octal string and the second parameter is not transferred, the return value is 0

    JavaScript The middle number has decimal system . octal . Hexadecimal . With "0" It starts with octal ,"0x" or "0X" It starts with hexadecimal . parseInt It's used to put words ...

  7. ***php analysis JSON Two dimensional array string (json_decode The second argument to the function True and False The difference between )

    Data in the request body of the client :[{"msg_id": 1, "msg_status": "HAS_READ" }, { "msg_id& ...

  8. 【 translate 】addEventListener The second parameter

    This is the original link :Our Backwards DOM Event Libraries browser APIs Let's briefly review the interfaces that each browser provides to bind events . IE The browser provides element.attachEve ...

  9. Listen The meaning of the second parameter

    Today, I will mainly review listen The meaning of the second parameter of . Now it's all about using frameworks to write business code . I really don't pay attention to the most basic socket The meaning of the function . It's almost forgotten .~~~  Pick it up slowly .  Mainly looking at redi ...

Random recommendation

  1. Need to know about struct Some things about

    Preface When refactoring code , You'll encounter methods with long arguments , You need to use “ Introduce parameter object ” To encapsulate these parameters . Most of the time , These parameters are simple types , Moreover, the space occupied by the values of all parameters is not very large , Is it really good to use objects at this time ? The property of an object is heap ...

  2. js in cookie


  3. MySQL Integrity language

    The article is original by the author , Unauthorized , Prohibited reproduced .    -Sun Yat-sen University Feng Xingwei experiment 3: Integrity language The integrity language experiment includes 3 Experimental projects , among 2 It's a compulsory project ,1 An elective project . The experiments of this experiment ...

  4. iOS Development Use RMStore Simplify internal purchase code + Internal purchase order verification

    Now a lot of app All of them add in app purchase , There are many tutorials about the generation and setting of Apple certificate on the Internet , I won't go into details here , Recommend a few personal feel that the more detailed website : ...

  5. JAVA- Database connection 【 turn 】

    SqlServer.Oracle.MySQL The connection of , Except for the address and driver package , Everything else is the same . 1 public String urlString="jdbc:sqlserver://localh ...

  6. Some small trick~

    It will save a lot of time to make prime table first when doing qualitative factorization

  7. Objective-C classification (category) And extend (Extension)

    1. classification (category) Use Object-C Classification of , It's a compile time tool , Allows us to extend a class by adding methods to it ( But by category Cannot add new instance variables ), And we don't need to access the code in the class to ...

  8. Eclipse Common hotkeys

    ,Ctrl+D Delete the selected lines ,Alt+ Up and down arrow Move the selected code block ,Alt+ Left and right arrows Back off Forward ,Alt+Shift+ Up and down arrow Copy the selected code block ,sysout+Ctrl space Generate Sys ...

  9. Angular Developer Guide ( One ) Introduction

    What is? Angular AngularJS It's dynamic Web The structural framework of the application . It allows you to use HTML As template language , And allows you to extend HTML The grammar is clear , Succinctly express the components of an application .AngularJS Data binding and ...

  10. bootcamp Partition _BOOTCAMP Failed to delete partition

    mac With a dual system ,Mac OS X Too little memory allocated , Leading to the use of catons , To delete windows System . In the delete windows when   “ Your disk cannot be restored to a single partition ” Solution : 1. restart Mac, And press ...