我写了一个小程序来习惯memset()操作:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
int main()
{
int arr[10], i;
int t = INT_MAX;
memset(arr, t, sizeof(arr));
for (i = 0; i < 10; i++)
printf("%d\t",arr[i]);
printf("%d",t);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上述计划的结果是:
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
2147483647
Run Code Online (Sandbox Code Playgroud)
memset()上述程序的行为是什么?为什么要将数组元素设置为-1?