C语言 如何在C上设置无符号字符数组的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18259848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:10:45  来源:igfitidea点击:

How to set value of unsigned char array on C

cstring

提问by perrohunter

I have an unsigned char array of size 64 that i want to change value of at runtime, however all my attemps have failed miserably, what am I doing wrong?

我有一个大小为 64 的无符号字符数组,我想在运行时更改它的值,但是我所有的尝试都失败了,我做错了什么?

int main() {
  unsigned char buffer[64]={0xef,0xaa,0x03,0x05,0x05,0x06,0x07,0x08,......};
  buffer = {0x01,0x04,0xa0,0xb0,0xde,0x00,.....}; //fails

  return 0;
}

EDIT: I don't want to fill with zero's the array buffer, I want to place a new value

编辑:我不想用零填充数组缓冲区,我想放置一个新值

回答by

Another solution would be the use of almighty memcpy()and C99 compound literals:

另一种解决方案是使用全能memcpy()和 C99 复合文字:

memcpy(array, (int []){ 0x00, 0x01, 0x02 }, sizeof array);

Believe it or not, this works.

信不信由你,这行得通

回答by CS Pei

    for (int i = 0; i < 64; ++i) buffer[i] = 0x00;

or in C++ (11 or later), you can use std::fill_n or std::generate_n

或在 C++(11 或更高版本)中,您可以使用 std::fill_n 或 std::generate_n

     std::fill_n(buffer, 64, 0x00);

or

或者

      for (auto &b : buffer) b = 0x00;

回答by Jacob Pollack

From your commentI see you do not want to access elements of the array. If not, then here is another solution to your problem.

从您的评论中,我看到您不想访问数组的元素。如果没有,那么这里是您问题的另一种解决方案。

You could declare the buffer on the memory pool. Hence,

您可以在内存池上声明缓冲区。因此,

unsigned char *buffer = malloc( sizeof( unsigned char ) * 64 );

... and then if you ever wanted to replace all of the elements of the array (as you have attempted to do using the array initialization syntax), it would be done as follows:

...然后如果您想替换数组的所有元素(正如您尝试使用数组初始化语法所做的那样),可以按如下方式完成:

memset( buffer, 0x00, sizeof( unsigned char ) * 64 ); // To replace buffer = { 0x00, ..., 0x00 };.
memset( buffer, 0, sizeof( unsigned char ) * 64 ); // To replace buffer = '0...0';.

Legacy:

遗产:

If you wanted to use an array declared on the stack then you would need to mutate it one element at a time using the square brackets [ ]. It would be done as follows:

如果您想使用在堆栈上声明的数组,则需要使用方括号一次改变一个元素[ ]。将按如下方式进行:

for ( int i = 0; i < 64; i++ ) {
  buffer[ i ] = val; // where "val" is some value.
}

回答by dasblinkenlight

Use std::memset:

使用std::memset

memset(buffer, 0, sizeof(buffer));

There's also bzero, but it's a legacy function, so it shouldn't be used in new development.

还有bzero,但它是一个遗留函数,所以不应该在新的开发中使用它。

回答by Pierre Fourgeaud

You can change the values of the element in two ways :

您可以通过两种方式更改元素的值:

unsigned char buffer[64]={0xef,0xaa,0x03,0x05,0x05,0x06,0x07,0x08,......};

buffer[0] = 0;
buffer[1] = 15;
// etc ...

// C++11 for-loop range works fine to :
for ( auto &c : buffer )
    c = 0;

Or after that, you can use function like : memset, std::fill_n:

或者在那之后,您可以使用像 : memset, std::fill_n:

memset( buffer, 0, sizeof(buffer) );
std::fill_n( buffer, 64, 0x00 );

回答by moooeeeep

You could use snprintf:

你可以使用snprintf

#include <stdio.h>

int main() {
  unsigned char buffer[64]={0};
  snprintf(buffer, 64, "Hello World!\x0D\x0A");
  printf(buffer);
}

Output:

输出:

$ ./a.out | hexdump -C
00000000  48 65 6c 6c 6f 20 57 6f  72 6c 64 21 0d 0a        |Hello World!..|
0000000e

回答by bames53

Initialization and assignment are two different operations, despite similarity in the syntax.

尽管语法相似,初始化和赋值是两种不同的操作。

int x[4] = { 1, 2, 3, 4}; // initialization
x = { 1, 2, 3, 4}; // assignment

Additionally, raw arrays in C and C++ behave strangely and in particular there's a special rule that says that they are not assignable:

此外,C 和 C++ 中的原始数组的行为很奇怪,特别是有一条特殊规则表明它们不可赋值:

int x[4];
int y[4];

y = x; // error, arrays are not assignable

So even if you create an array temporary object you cannot simply copy that into another array object using the assignment operator.

因此,即使您创建了一个数组临时对象,您也不能简单地使用赋值运算符将其复制到另一个数组对象中。

Instead, if you are set on using raw arrays, you have to set each array element individually, using memcpy, fill, etc.

相反,如果是在使用原始阵列设置,必须单独设置每个阵列元件使用memcpyfill等等。



A better solution is to use C++ and a type other than raw arrays which does allow assignment, such as std::array<unsigned char, 64>:

更好的解决方案是使用 C++ 和允许赋值的原始数组以外的类型,例如std::array<unsigned char, 64>

std::array<unsigned char, 64> buffer = {0xef,0xaa,0x03,0x05,0x05,0x06,0x07,0x08};
buffer = {}; // zeros out the array

The std::arraytemplate just generally behaves consistently like a normal object whereas raw arrays in C and C++ have all these very strange behaviors and special cases in the language specs. You should avoid using raw arrays.

std::array模板只是一般的行为始终像一个正常的对象,而在C和C原料阵列++这种语言的规范所有这些非常奇怪的行为和特殊情况。您应该避免使用原始数组。



Unfortunately C does not have any alternative to raw arrays.

不幸的是,C 没有任何替代原始数组的方法。

回答by chux - Reinstate Monica

Since that are some compiler issues, try a simple usage of memcpy().

由于这是一些编译器问题,请尝试简单使用memcpy().

#include <string.h>
int main() {
  unsigned char bufferTo[  64]={0xef,0xaa,0x03,0x05,0x05,0x06,0x07,0x08,......};
  unsigned char bufferFrom[64]={0x01,0x04,0xa0,0xb0,0xde,0x00,.....};
  memcpy(bufferTo, bufferFrom, 64);
  return 0;
}