[英]How to initialize arrays in 2 ways using loop and memset?
I am trying to answer a question in my C programming book, but I am not sure if my answer is correct. 我试图在我的C编程书中回答一个问题,但是我不确定我的回答是否正确。
The book doesn't provide any answers though. 这本书没有提供任何答案。 I am new to C programming, and any help will be appreciated.
我是C编程的新手,我们将不胜感激。
Question: 题:
Assume you have declared an array as follows: 假设您已声明一个数组,如下所示:
float data[1000];
Show two ways to initialize all elements of the array to 0. 显示两种将数组的所有元素初始化为0的方法。
Use a loop and an assignment statement for one method, and the memset () function for the other. 对一种方法使用循环和赋值语句,对另一种方法使用memset()函数。
my current code : 我当前的代码 :
#include <stdio.h>
float data[1000];
main(){
int count;
for (count = 0; count < 1000; count++){
scanf("%f", &data[count]);
}
for (count = 0; count < 1000; count++){
printf("Array %i: %f\n", count, data[count]);
}
}
A few points to help you get started: 以下几点可帮助您入门:
0
. 0
。 Scanf
requires you to input all values. Scanf
要求您输入所有值。 This isn't necessary as you could just set them to 0
with data[count]=0.0f;
data[count]=0.0f;
将它们设置为0
data[count]=0.0f;
inside of that for loop. memset
is a function that will do something similar for you (including the for loop). memset
是一个可以为您做类似事情的函数(包括for循环)。 Have a look at the documentation of memset
: memset
的文档: memset
记忆集
void * memset ( void * ptr, int value, size_t num );
void * memset(void * ptr,int值,size_t num);
Fill block of memory Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).
填充内存块将ptr指向的内存块的前num个字节设置为指定值(解释为无符号字符)。
Parameters
参量
- ptr: Pointer to the block of memory to fill.
ptr:指向要填充的内存块的指针。
- value: Value to be set.
value:要设置的值。 The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.
该值以int形式传递,但是函数使用该值的无符号char转换来填充内存块。
- num: Number of bytes to be set to the value.
num:要设置为该值的字节数。 size_t is an unsigned integral type.
size_t是无符号整数类型。
You should notice that memset only works with bytes. 您应该注意,memset仅适用于字节。 So you can use it to set a float to
0
, as it consists of 4 bytes that are all 0
, but you cannot set it to 1
for instance. 因此,您可以使用它将float设置为
0
,因为它由4个字节组成,这些字节全为0
,但是例如不能将其设置为1
。 But as notices by other users, this just happens to be so on most hardware. 但是,正如其他用户所注意到的那样,在大多数硬件上也是如此。
memset(data,0,sizeof(data));
可用于将数组的所有元素分配为0
Show two ways to initialize all elements of the array to 0. Use a loop and an assignment statement for one method, and the memset () function for the other.
显示两种将数组的所有元素初始化为0的方法。一种方法使用循环和赋值语句,另一种方法使用memset()函数。
1) Using for loop and assignment statement 1)使用for循环和赋值语句
for(i = 0; i<1000; i++)
data[i] = 0.0f;
2) Using memset function 2)使用记忆集功能
memset(&data,0,sizeof(data));
Short and simple way supported by c99 spec c99规范支持的简短方法
float data[1000] = {0.0f};
declare array like this. 这样声明数组。
Actually there are at least three methods.:) 实际上至少有三种方法:)
// first
float data[1000] = { 0.0f };
// second
size_t i;
for ( i = 0; i < 1000; i++ ) data[i] = 0.0f;
// third
memset( data, '\0', sizeof( data ) );
You can also add one more method in C99 您还可以在C99中添加另一种方法
// forth
float data[1000] = { [0] = 0.0f };
In C++ you could use also the following declaration 在C ++中,您还可以使用以下声明
float data[1000] = {};
or 要么
float data[1000] {};
I don't think there is a need for scanf("%f",&data[count]);
我认为不需要
scanf("%f",&data[count]);
instead you can directly assign it with data[count]=0;
相反,您可以直接将其分配为
data[count]=0;
and if you use the memset()
function you can set value to zero directly to an array memset(data,'0',1000);
如果使用
memset()
函数,则可以将值直接设置为零,以将其设置为数组memset(data,'0',1000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.