memset function in c language

后端 未结 3 605
刺人心
刺人心 2021-01-31 11:21

I am studying the memset function now, but all the examples are regarding to char array as following:

char a[100];
memset(a, 0, 100);

it will s

3条回答
  •  天涯浪人
    2021-01-31 11:34

    Yes, it can apply to any memory buffer, but you must input the correct memory buffer size ... memset treats any memory buffer as a series of bytes, so whether it's char, int, float, double, etc, doesn't really matter. Keep in mind though that it will not set multi-byte types to a specific non-zero value ... for example:

    int a[100];
    memset(a, 1, sizeof(a));
    

    will not set each member of a to the value 1 ... rather it will set every byte in the memory buffer taken up by a to 1, which means every four-byte int will be set to the value 0x01010101, which is not the same as 0x00000001

提交回复
热议问题