You can do the same thing in C, but you should declare it of type char[], not char*, so that you can get its size with the sizeof operator:

char myArray[] = { 0x00, 0x11, 0x22 };
size_t myArraySize = sizeof(myArray);  // myArraySize = 3
Answer from Adam Rosenfield on Stack Overflow
๐ŸŒ
IncludeHelp
includehelp.com โ€บ c-programs โ€บ initialising-byte-array-with-decimal-octal-and-hexadecimal-numbers-in-c.aspx
Initialising byte array with Decimal, Octal and Hexadecimal numbers in C
September 6, 2018 - #include <stdio.h> int main() { // declaring array, initialising with decimal values unsigned char arr1[] = {10, 20, 30, 40, 50}; // declaring array, initialising with octal values unsigned char arr2[] = {010, 077, 023, 045, 057}; // declaring array, initialising with hexadecimal values unsigned char arr3[] = {0x10, 0xAA, 0x67, 0xA1, 0xFF}; int i; // printing the numbers printf("arr1...\n"); for (i = 0; i < 5; i++) printf("%d ", arr1[i]); printf("\n"); // printing the numbers printf("arr2...\n"); for (i = 0; i < 5; i++) printf("%o ", arr2[i]); printf("\n"); // printing the numbers printf("arr3...\n"); for (i = 0; i < 5; i++) printf("%X ", arr3[i]); printf("\n"); return 0; }
Discussions

initialization - Initialize C array with bytes and string literals - Stack Overflow
I would like to initialize a byte array with some byte values and a string literal in one go. Essentially the array is serialized representation of data structure like struct foo { uint8 param1; More on stackoverflow.com
๐ŸŒ stackoverflow.com
Looking for method to initialize an array of structures (type contains some constant vectors)
C has a uniform initialization syntax for both arrays and structs, so you can use the following. struct a_type { uint8_t x; uint8_t y[8]; uint8_t z[8]; }; struct a_type structs[8] = { { .x = 1 , .y = "ABC" , .z = { 0, 1, 2, 3, 4, 5, 6, 7 } } , { .x = 2 , .y = "DEF" , .z = { 8, 9, 10, 11, 12, 13, 14, 15 } } , { .x = 3 , .y = "GHI" , .z = { 16, 17, 18, 19, 20, 21, 22, 23 } } , { .x = 4 , .y = "JKL" , .z = { 24, 25, 26, 27, 28, 29, 30, 31 } } , { .x = 5 , .y = "MNO" , .z = { 32, 33, 34, 35, 36, 37, 38, 39 } } , { .x = 6 , .y = "PQR" , .z = { 40, 41, 42, 43, 44, 45, 46, 47 } } , { .x = 7 , .y = "STU" , .z = { 48, 49, 50, 51, 52, 53, 54, 55 } } , { .x = 8 , .y = "VWX" , .z = { 56, 57, 58, 59, 60, 61, 62, 63 } } }; In cases where the type of the initialization list cannot be inferred, you stick the type in parens before the initializer list: (struct a_type){ 1, "ABC", (uint8_t[8]){ 1, 2, 3, 4, 5, 6, 7 }} Note that this is not a type cast, despite it looking like one. It's a compound literal syntax. See Godbolt for demonstration. We don't necessarily need to specify the field names if you want to write it more tersely: struct a_type structs[] = { { 1, "ABC", { 0, 1, 2, 3, 4, 5, 6, 7 } } , { 2, "DEF", { 8, 9, 10, 11, 12, 13, 14, 15 } } , { 3, "GHI", { 16, 17, 18, 19, 20, 21, 22, 23 } } , { 4, "JKL", { 24, 25, 26, 27, 28, 29, 30, 31 } } , { 5, "MNO", { 32, 33, 34, 35, 36, 37, 38, 39 } } , { 6, "PQR", { 40, 41, 42, 43, 44, 45, 46, 47 } } , { 7, "STU", { 48, 49, 50, 51, 52, 53, 54, 55 } } , { 8, "VWX", { 56, 57, 58, 59, 60, 61, 62, 63 } } }; More on reddit.com
๐ŸŒ r/cprogramming
11
2
March 17, 2026
How to create a byte array in C++? - Stack Overflow
It also failed with the same error. However, I noticed I can call other built in type arrays in this way, for example, an int array. Why is this happening to byte array? How to solve this? I would like to assign the values in the cpp file. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Initializing a vector<BYTE> from existing BYTE array - Post.Byes
The reason behind this is because I have an old API that sends me a BYTE array (that was read from a file) and I want it in a vector. I can't afford duplicating the BYTE array that I receive because it can get to huge sizes. Thanks. ... Hello, I wish to initialize a vector from an existing ... More on post.bytes.com
๐ŸŒ post.bytes.com
๐ŸŒ
HatchJS
hatchjs.com โ€บ home โ€บ how to initialize a byte array in c
How to Initialize a Byte Array in C
January 5, 2024 - The most common way is to use the `new` keyword. For example, the following code creates a byte array with 10 elements: ... You can also initialize a byte array with a specific value by using the `byte[].Fill` method.
Top answer
1 of 2
2

You will have to initialise the byte array using a function like this:

uint8 *initByteArray(const struct foo *foo)
{
    unsigned slen = foo->string ? strlen(foo->string) : 0;
    assert(slen <= 255);
    uint8 *array = malloc(slen + 3);
    unsigned i = 0;
    array[i++] = foo->param1;
    array[i++] = (uint8)slen;
    if (slen) {
        strncpy(&array[i], foo->string, slen);
        i += slen;
    }
    array[i++] = foo->param2;
    return array;
}

If the struct contains any integers with a length greater than 1 (i.e. uint16, etc) then you will also need to consider their endianness during encoding.

2 of 2
0

C allows string literals (also in initialisers) to be broken up; the compiler will paste them for you.

So, you could do:

char unit1[8] = "\x40" "\x05" "Hello" "\x3e";
char unit2[9] = "\x40" "\x06" "World!" "\x3e";

char unitBoth[] = "\x40" "\x05" "Hello" "\x3e"
                  "\x40" "\x06" "World!" "\x3e";

The unitBoth[] array has no defined size; it is just one byte longer (the nul byte) than the sum of the two separate arrays.


#include <stdio.h>
#include <string.h>

int main(void)
{
unsigned idx, len;

printf("Unit1 Size = %zu\n", sizeof unit1);
for (idx=0; idx < sizeof unit1; idx++) {
        printf("%u : %02x\n" , idx , 0xff & unit1[idx] );
        }

printf("\nUnit2 Size = %zu\n", sizeof unit2);
for (idx=0; idx < sizeof unit2; idx++) {
        printf("%u : %02x\n" , idx , 0xff & unit2[idx] );
        }

printf("\nUnitBoth Size = %zu\n", sizeof unitBoth);
for (idx=0; idx < sizeof unitBoth; idx++) {
        printf("%u : %02x\n" , idx , 0xff & unitBoth[idx] );
        }

return 0;
}

And of course you could create a program or script to generate the definitions. (and include them in your main program, possibly in global scope)

๐ŸŒ
ByteHide
bytehide.com โ€บ blog โ€บ instantiating arrays in c#: detailed tutorial
Instantiating Arrays in C#: Detailed Tutorial (2026)
December 11, 2023 - Each hexadecimal number is a byte, and we just created a byte array containing five bytes! Prepopulating arrays is an excellent way to equip your array with pre-set or default values. This special ability allows arrays to leaps into action right off the bat! Suppose we are developing a game wherein a player earns points by finding words hidden in a pool of letters. To initiate the game, weโ€™d like a list of words already set up:
๐ŸŒ
C For Dummies
c-for-dummies.com โ€บ blog
Initializing Arrays in C23 | C For Dummies Blog
January 13, 2024 - You can initialize an array in the C programming languages in three ways: Not at all, only some elements, or all of the elements. Some compilers offer a fourth way, which initializes all elements to the same value. With the C23 standard, yet another way to initialize an array is possible.
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/cprogramming โ€บ looking for method to initialize an array of structures (type contains some constant vectors)
r/cprogramming on Reddit: Looking for method to initialize an array of structures (type contains some constant vectors)
March 17, 2026 -

First post here, old-school C user for microcontrollers (using GCC in Eclipse-based SDK published by ST Micro).

I need to create and initialize an array of structures (these structures would end up in RAM, so not using the const declaration anywhere.

Each element (a structure) would contain a few integers and a few byte arrays (one expressed as ASCII characters, others are 8-bit integers.) Currently I create the structure (individual elements) and call a function to copy the elements into the structure which is one of N in an array, which is probably OK but makes the source code look clumsy(er).

This is roughly what I'd like to accomplish, but not sure how to code in C (please forgive the formatting and I suspect none of this would compile, but hopefully it conveys what I'm trying to accomplish.

this is one element of the example struct array:

struct a_type
{
uint8_t x;
uint8_t[8] y;
uint8_t[8] z;
}

This is the array of the structures (eight of these, for example:)

a_type structs[8]; // End up with eight of the above, each containing one byte scalar and two byte arrays of 8 elements each.

What I want to accomplish looks like this:

structs[0].x = 123; // Single uint8_t
structs[0].y = "ABCDEFGH"; // Each char/uint8_t, no zero terminator
structs[0].z = { 0, 1, 2, 3, 4, 5, 6, 7}; // Each are uint8_t

Grateful for any suggestions, requests for clarification, or criticism!

Dave
Top answer
1 of 3
4
C has a uniform initialization syntax for both arrays and structs, so you can use the following. struct a_type { uint8_t x; uint8_t y[8]; uint8_t z[8]; }; struct a_type structs[8] = { { .x = 1 , .y = "ABC" , .z = { 0, 1, 2, 3, 4, 5, 6, 7 } } , { .x = 2 , .y = "DEF" , .z = { 8, 9, 10, 11, 12, 13, 14, 15 } } , { .x = 3 , .y = "GHI" , .z = { 16, 17, 18, 19, 20, 21, 22, 23 } } , { .x = 4 , .y = "JKL" , .z = { 24, 25, 26, 27, 28, 29, 30, 31 } } , { .x = 5 , .y = "MNO" , .z = { 32, 33, 34, 35, 36, 37, 38, 39 } } , { .x = 6 , .y = "PQR" , .z = { 40, 41, 42, 43, 44, 45, 46, 47 } } , { .x = 7 , .y = "STU" , .z = { 48, 49, 50, 51, 52, 53, 54, 55 } } , { .x = 8 , .y = "VWX" , .z = { 56, 57, 58, 59, 60, 61, 62, 63 } } }; In cases where the type of the initialization list cannot be inferred, you stick the type in parens before the initializer list: (struct a_type){ 1, "ABC", (uint8_t[8]){ 1, 2, 3, 4, 5, 6, 7 }} Note that this is not a type cast, despite it looking like one. It's a compound literal syntax. See Godbolt for demonstration. We don't necessarily need to specify the field names if you want to write it more tersely: struct a_type structs[] = { { 1, "ABC", { 0, 1, 2, 3, 4, 5, 6, 7 } } , { 2, "DEF", { 8, 9, 10, 11, 12, 13, 14, 15 } } , { 3, "GHI", { 16, 17, 18, 19, 20, 21, 22, 23 } } , { 4, "JKL", { 24, 25, 26, 27, 28, 29, 30, 31 } } , { 5, "MNO", { 32, 33, 34, 35, 36, 37, 38, 39 } } , { 6, "PQR", { 40, 41, 42, 43, 44, 45, 46, 47 } } , { 7, "STU", { 48, 49, 50, 51, 52, 53, 54, 55 } } , { 8, "VWX", { 56, 57, 58, 59, 60, 61, 62, 63 } } };
2 of 3
1
First, you can't do a string initialization like uint8_t string[8] = "8charstr"; The compiler will complain that the storage is too small for the value, because "8charstr" requires 9 bytes, because it includes the null terminator. Best way to achieve this goal is: uint8_t string[8] = { '8', 'c', 'h', 'a', 'r', 's', 't', 'r' };
๐ŸŒ
cppreference.com
en.cppreference.com โ€บ c โ€บ language โ€บ array_initialization
Array initialization - cppreference.com
L-prefixed wide string literals can be used to initialize arrays of any type compatible with (ignoring cv-qualifications) wchar_t ยท Successive bytes of the string literal or wide characters of the wide string literal, including the terminating null byte/character, initialize the elements of the array:
๐ŸŒ
Quora
quora.com โ€บ What-is-a-byte-array-in-C
What is a byte array in C? - Quora
Answer (1 of 4): Never heard this term but if it is: * Array of elements of 1 byte each then make an array of [code ]char[/code] since the size of [code ]char[/code] is 1 byte each (on most of the systems). * If it is an array having it's complete size as 1 byte and you want to manipulate ever...
๐ŸŒ
Atomic Spin
spin.atomicobject.com โ€บ byte_array-c-built-in-type
byte_array โ€“ The Missing Built-in Type for C - Atomic Spin
December 22, 2016 - Though the concept is applicable to arrays of any type. ... typedef struct _ByteArray { size_t len; /**< Number of bytes in the `data` field. */ uint8_t* data; /**< Pointer to an allocated array of data bytes.
๐ŸŒ
Code with C
codewithc.com โ€บ code with c โ€บ blog โ€บ c++ byte array: handling byte-level data
C++ Byte Array: Handling Byte-Level Data - Code With C
January 11, 2024 - Creating C++ Byte ArrayDefining a Byte Array in C++Initializing a Byte Array in C++Manipulating C++ Byte ArrayReading from a Byte Array in C++Writing to a Byte Array in C++Converting C++ Byte ArrayConverting Byte Array to String in C++Converting String to Byte Array in C++Operating on C++ Byte ...
๐ŸŒ
Runebook.dev
runebook.dev โ€บ en โ€บ docs โ€บ c โ€บ language โ€บ array_initialization
The Ultimate Guide to C Array Initialization
// For integers, this works for 0, but not for other numbers like 1. memset(data, 0, sizeof(data)); printf("The first element is: %d\n", data[0]); // Prints 0 return 0; } Note memset() is great for zero-initialization, but for setting every ...
๐ŸŒ
Post.Byes
post.bytes.com โ€บ home โ€บ forum โ€บ topic โ€บ initializing a vector from existing byte array
Initializing a vector<BYTE> from existing BYTE array - Post.Byes
This is a contradiction. You can't use a vector to manage an array that's not in the vector. Also, I don't understand your duplication problem. The BYTE array is on the heap so you copy it to a vector<BYTE> that's also on the heap and then delete the BYTE array.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 74136804 โ€บ how-to-declare-and-initialize-an-array-of-random-bytes-in-c
How to declare and initialize an array of random bytes in C? - Stack Overflow
I can create an array of bytes like below: test_array[3] = {0x01, 0x02, 0x03}; I want an array of bytes like above, but with random bytes. For example, I just declare that I need an array of 10 byt...
๐ŸŒ
Reddit
reddit.com โ€บ r/cprogramming โ€บ initialize a char array?
r/cprogramming on Reddit: Initialize a char array?
April 23, 2024 -

How come this works:

Char name[20] = "Mary";

But this doesn't.

Char name[20];

Name[20] = "Mary";

Am I correct in that the above fails because I'm telling it to put "Mary" in the name[20] element? Is that how it's interpreted?

Is there a way to initialize char name[20] array separately from the declaration without using srtcpy()? Just wondering

Thanks

๐ŸŒ
Reddit
reddit.com โ€บ r/cprogramming โ€บ any good detailed resources on initializing arrays and the values we can give them?
r/cprogramming on Reddit: Any good detailed resources on initializing arrays and the values we can give them?
January 22, 2023 -

I came across an issue where a char array wasn't initialised and the fix was to put "" into the value e.g. Char array [100] = "";

I don't get why this worked, i suspect it was just filling with garbage before, so i have looked online for some good explanations but have only come across a tonne of articles explaining assigning string literals to the array inside curly braces.

Are there any good resources explaining the different ways we can initialize arrays? I know a few but am definitely missing some.l and a complete list would be excellent.

Thankyou

Top answer
1 of 2
3
Array elements can be initialized with an initializer list: xxxx array[N] = {element 1, element 2, ...}; If there are fewer than N elements in the initializer list, the remainder will be zero-initialized. String literals result in null-terminated character arrays. Doing char array[100] = ""; is like char array[100] = {"\0"}; which is like char array[100] = {0}; which zero-initializes the entire array.
2 of 2
2
If you need to initialize all values of an array to zero, the you can use the function: memset(array, 0, sizeof(array)); It's worth noting that the function memset() sets the values of the individual bytes, not the elements. Since all bytes being zero generally also represent a value being zero, so that sets all elements to zero. If you want to initialize all elements to a specific value other than zero, then you need a for loop: for (int i = 0; i < num_elements; i++) { array[i] = value; } There isn't really an way to do that with a literal in standard C, but compilers may have their own ways (which are not standard and might not work in other compilers. In GCC you can do something like this: int array[100] = {[0 ... 99] = value}; It's important to note that this might give an error on other compilers. But this way works when you are declaring and initializing array in a header. And I also would like to emphasize that you can't call functions or use for loops outside a function, so maybe it might be a good idea to create a function that does the initialization of the array, and call them before using the array.