简体   繁体   English

如何在C上设置无符号char数组的值

[英]How to set value of unsigned char array on C

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的无符号char数组,我想在运行时更改其值,但是我的所有尝试都不幸失败了,我在做什么错?

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 编辑:我不想用零填充的数组缓冲区,我想放置一个新值

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 . 信不信由你, 这行得通

    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;

From your comment I 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.
}

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 ,但这是一个遗留函数,因此不应在新开发中使用它。

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 : 或之后,您可以使用以下函数: memsetstd::fill_n

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

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

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::array template 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. 通常, std::array模板的行为就像普通对象一样,而C和C ++中的原始数组在语言规范中具有所有这些非常奇怪的行为和特殊情况。 You should avoid using raw arrays. 您应该避免使用原始数组。


Unfortunately C does not have any alternative to raw arrays. 不幸的是,C不能替代原始数组。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM