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
Newest Questions - Stack Overflow
Stack Overflow | The World’s Largest Online Community for Developers 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
Any good detailed resources on initializing arrays and the values we can give them?
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. More on reddit.com
🌐 r/cprogramming
4
8
January 22, 2023
🌐
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.
🌐
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...
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)

🌐
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:
Find elsewhere
🌐
C For Dummies
c-for-dummies.com › blog
Initializing Arrays in C23 | C For Dummies Blog
January 13, 2024 - If the standard were to suddenly mandate that local variables had to be initialized, this would force the compiler to emit something like a memset(RSP, 0, 8); call to ensure that all those stack bytes between RSP and RSP + 7 were set to zero (before the “real body” of the function was entered).
🌐
Runebook.dev
runebook.dev › en › docs › c › language › array_initialization
The Ultimate Guide to C Array Initialization
#include <stdio.h> #include <string.h> int main() { int data[100]; // This sets all bytes of the array to 0. // Be careful! It sets a byte value, not an integer value. // 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 element to a non-zero value like 5, a loop is the correct and safest method.
🌐
The Geek Stuff
thegeekstuff.com › 2011 › 12 › c-arrays
C Arrays Basics Explained with 13 Examples
March 6, 2014 - In the above declaration/initialization, we have initialized array with a series of character followed by a ‘\0’ (null) byte.
🌐
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' };
🌐
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.
🌐
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.
🌐
Jsommers
jsommers.github.io › cbook › arraysstrings.html
5. Arrays and Strings — The Book of C (version 2022.08)
Note that we need to specify the number of bytes we want to set to 0, thus we say sizeof(int) multiplied by the number of array elements. It's always good practice to use sizeof, even if you think you can assume that the size of an int is 4. Don't make that assumption; use sizeof. One last way that array contents can be initialized is to use C initializer syntax.
🌐
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...
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 6995-initialize-array.html
Initialize an array
Bottom example - in 16 bit real mode programming an integer is 2 bytes so you must multiply 64000 by 2, or shift left by 1, to get the correct size of the memory. 64000 integers requires 128000 bytes since each integer is 2 bytes or 16 bits. Only use a for loop when you are initializing an array with different default values since this cannot be done using memset or other fast memory functions or w/o using assembly.
🌐
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.