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 OverflowYou 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.
C help needed. (string to BYTE array)
array byte
Ask them to refund your tuition.
More on reddit.comHow to create a byte array in C++? - Stack Overflow
How do I convert a byte array into an integer in c?
Hello:
Would someone please point me to the solution for this? ( I am a C newbie )
I am running a C program with an Input argument .
This argument (argv[1]) is a string containing "1112131415e0",
I need to have this string stored in a BYTE array (container1) such that is the same as if I wrote :
BYTE container1[] = { 0x11, 0x12, 0x13, 0x14,0x14,0xe0}
I don't want to convert 11 to hex.. I just want to add the 0x and have it stored.
Thanks a lot!
Hello,
In my college slide, it says
unsigned long studentIDs[200];
will reserve 4 bytes for the variable 'studentIDs' and spaces for 200 longs.
so, the next defined variable would be 804(4+200x4) further down in memory.
I don't get it. I think address of studentIDs variable is equal to studentIDs[0].
So, I think it only occupy 200x4 bytes in computer memory from the defined array.
Am I missing something?
******update************
I asked my professor through email and got a reply like the following.
He said studenIDs is a reference type. so it stores a reference to a chunk of storage. so it needs 4 bytes for itself.
I still don't understand.
*****another update********
I told my professor again I didn't get it. He explained variable studentIDs and its value is stored in "symbol table" maintained by the compiler. Actually the slide was not from my current professor. He believes the slide author meant that the extra 4 bytes shown in the slide is referring to the storage used to maintain its symbol table entry.
I feel pretty content now. I guess I will learn in detail in next semester in the compiler course.
Ask them to refund your tuition.
No, your college is wrong. "studentIDs" is of type unsigned long[200]. If you do sizeof you will get 200*sizeof(unsigned long). There can not be (by the language definition) anything else.
The prof most likely is confused over the difference between arrays and pointers. They are not the same.
Try
class MissileLauncher
{
public:
MissileLauncher(void);
private:
unsigned char abc[3];
};
or
using byte = unsigned char;
class MissileLauncher
{
public:
MissileLauncher(void);
private:
byte abc[3];
};
**Note: In older compilers (non-C++11) replace the using line with typedef unsigned char byte;
If you want exactly one byte, uint8_t defined in cstdint would be the most expressive.
http://www.cplusplus.com/reference/cstdint/
I have a byte array that looks like [E1,DD,5,2] coming from a CAN bus.
The hex string should be something like: "0205DDE1" which equals a value of 33938917.
I have no idea what I'm doing wrong, my code so far looks like:
int32_t val = ((arr[3] << 32) | (arr[2] << 16) | (arr[1] << 8) | arr[0]);