So how to set INT_MAX to an array of int with memset?
时间: 2024-02-29 19:51:38 浏览: 101
`INT_MAX` is a constant that represents the maximum value that an `int` can hold. If you want to set an array of `int` to `INT_MAX` using `memset`, you should convert `INT_MAX` to a binary representation that fits the size of an `int`, and pass that value to `memset`.
For example, if `int` is 4 bytes (32 bits) and `INT_MAX` is defined as `2147483647`, you can convert `INT_MAX` to the binary representation `01111111 11111111 11111111 11111111`, which is the largest possible positive value for an `int`. To set an array of `int` to this value, you can use the following code:
```c++
int arr[10];
memset(arr, -1, sizeof(arr)); // set all elements to -1 (binary: 11111111 11111111 11111111 11111111)
memcpy(arr, &INT_MAX, sizeof(int)); // set all elements to INT_MAX (binary: 01111111 11111111 11111111 11111111)
```
Note that you should use `memcpy` instead of `memset` to set the array to `INT_MAX`, because `memset` only sets the bytes to a specific value, whereas `memcpy` copies the binary representation of a value to a memory location.
阅读全文