#include <stdio.h>
#include <stdlib.h>
#include <cstring>
int main()
{
int size = 5 ;
int* c = (int*)calloc(size,sizeof(int));
memset(c,0,size*sizeof(int)); // when I input 1 or another value it doesn't work
for (int i=0; i<size;i++){
printf("values %d\n", c[i]);
}
free(c);
}
本程序的输出如下所示;
value 0
value 0
value 0
value 0
value 0
但是,如果我将0值更改为1:
value 16843009
value 16843009
value 16843009
value 16843009
value 16843009
我用calloc分离了内存.我想给我用Memset函数分配的这个内存地址赋值.当我在这里赋值为0时,赋值成功完成.我显示的是一个随机值,而不是我在该值之外给出的值.如何使用Memset函数成功执行此任务?