I'm debugging a network application.
I have to simulate some of the data exchanged in order for the application to work. In C++ you can do something like
char* myArray = { 0x00, 0x11, 0x22 };
However, I can't seem to find a C equivalent for this syntax.
Basically I just want to fill an array with hard coded values.
byte array in C An unsigned char can contain a value from 0 to 255, which is the value of a byte. In this example, we are declaring 3 arrays – arr1, arr2, and arr3, arr1 is initialising with decimal elements, arr2 is initialising with octal numbers and arr3 is initialising with hexadecimal numbers.
You can simply iterate the byte array and print the byte using System. out. println() method.
A byte array is simply an area of memory containing a group of contiguous (side by side) bytes, such that it makes sense to talk about them in order: the first byte, the second byte etc..
The default values of numeric array elements are set to zero, and reference elements are set to null. Since byte represents integer values from 0 to 255 , all elements are set to 0 in your authToken array.
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
Just for the sake of completeness, with C99 you can also use compound literals:
char *myArray = (char []) {0x00, 0x11, 0x22 };
If source code compatibility to C++ is a requirement, you better don't use this construct, because it is - afaik - not part of the C++ standard.
Try with:
char myArray[] = { 0x00, 0x11, 0x22 };
Doesn't
char myArray[] = {0x00, 0x01,0x02};
work?
Array Initialization
Array Initialization
char myArray[] = {0x00, 0x11, 0x22};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With