4

I would like to initialise an array with the value set in another array, like:

uint8_t array_1[] = {1, 2, 3};

uint8_t array_2[] = array_1;

Of course this would not work since array_1 is considered a pointer. What I'm trying to do is to be able to statically initialize array_2 with the value of array_1 without using memset.

Since array_1 and array_2 would in my case be constant global buffers, I believe there should be a way to do this, but I haven't figured out how, or by using defines, but I'd rather stick to the other solution if possible.

Thank you

10
  • 3
    If they are really both going to be constant global buffers, then maybe you don't actually need two of them. uint8_t* array_2 = array_1; Commented Jun 1, 2021 at 14:25
  • 3
    #define INITIAL_VALUES {1, 2, 3} Commented Jun 1, 2021 at 14:26
  • 1
    @pmg or an include file Commented Jun 1, 2021 at 14:27
  • 2
    You can't use memset to copy values from one array to another. This is what memcpy is for - of course, it is not static initialization. Commented Jun 1, 2021 at 14:32
  • 1
    @SergeyA he probably meant memcpy. Commented Jun 1, 2021 at 14:32

2 Answers 2

8

There is no particularly elegant way to do this at compile-time in C than using #define or repeating the initializer. The simplest versions:

uint8_t array_1[] = {1, 2, 3};
uint8_t array_2[] = {1, 2, 3};

or

#define INIT_LIST {1, 2, 3}

uint8_t array_1[] = INIT_LIST;
uint8_t array_2[] = INIT_LIST;

Though if you were using structs, you could do:

typedef struct
{
  int arr [3];
} array_t;

array_t array_1 = { .arr = {1,2,3} };
array_t array_2 = array_1;

But that only works if these are local objects and this is essentially equivalent to calling memcpy.

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

4 Comments

Wouldn't just array_t array_1 = { 1, 2, 3 }; work, or is that using some nasty Microsoft extension? (It does work on MSVC and clang-cl.)
@AdrianMole Yeah that works thanks to C's complex and kind of dysfunctional rules of initialization of "sub-objects", but gcc will generate a warning if you do. Alternatively you could also do array_t array_1 = { {1,2,3} };. I think designated initializers is the most readable form.
Sometimes, "dysfunctional" is nice! If only there were a way to use compound literals as typedefs and then use those to initialize arrays...
@AdrianMole Not so nice if you have typedef struct { int arr [3]; int x; } array_t; and then array_t array_1 = { 1,2,3 };. Now did I leave out the initialization of x on purpose (set to zero), or did I forget it?
-3

You can do it like this

uint8_t length = sizeof(array_1) / sizeof(array_1[0]);
for(uint8_t i = 0; i < length; i++) {
  array_2[i] = array_2[i]
}

3 Comments

hey @codingwith3dv, since it's statically allocated in my case, I do not have acces to any function. Furthermore I'm working in C, not C++, so no vectors available.
Where did you allocate memory for array_2? (your 1st solution)
@codingwith3dv your new answer is somewhat better, but still wrong. He want's to do the initialisation statically.

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.