0

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?

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

4
  • By the way, '0' != 0x00. Commented Aug 15, 2013 at 19:10
  • @ShafikYaghmour Any of them, mainly C, I know C++ devs could also know the answer Commented Aug 15, 2013 at 19:11
  • @Inspired so how could I set the values if I got "f7" and I want to store 0xf7 ? Commented Aug 15, 2013 at 19:11
  • @perrohunter what do you mean - got "f7"? A null-terminated string "f7", that represents a hexadecimal number? Then convert it to a number (strtol, lexical_cast or stringstream can help). Commented Aug 15, 2013 at 19:16

8 Answers 8

9

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

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

Believe it or not, this works.

Sign up to request clarification or add additional context in comments.

4 Comments

I was waiting for this answer :) I written incorrectly in comment.
@GrijeshChauhan I knew you were :)
@perrohunter Too bad. Upgrade your compiler to a post-1970 one.
@perrohunter compile your code with -std=c99 its will work very nice technique.
4
    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

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

or

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

1 Comment

my bad, std::fill_n is a C++ function. Also the range for is a C++ feature.
2

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

4 Comments

I don't want to access the elements, I want to change the value stored on buffer
@perrohunter But you can't assign to an array. It's not a modifiable lvalue. You have to use array subscripting in order to change its elements.
@perrohunter, added another solution. Take a look it achieves exactly what you want given those specific examples.
@JacobPollack What's the difference betwen the two calls to memset() in your code?
1

Use 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.

Comments

1

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( buffer, 0, sizeof(buffer) );
std::fill_n( buffer, 64, 0x00 );

Comments

0

You could use 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

Comments

0

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:

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.


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>:

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. You should avoid using raw arrays.


Unfortunately C does not have any alternative to raw arrays.

Comments

0

Since that are some compiler issues, try a simple usage of 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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.