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.
pebble watch - How to store byte arrays in C? - Stack Overflow
c++ - How do I create an array of byte arrays? - Stack Overflow
array byte
Ask them to refund your tuition.
More on reddit.comHow to store a byte array in C - Stack Overflow
You can allocate 12*96 byte of contiguous memory having 12 rows & 96 columns
char* image = (char*)malloc(sizeof(char)*12*96);
also, a global array will do just fine
char image[12][96];
From what I understand, you are recieving data row wise ie 96 bytes at a time:
char rcvd_data[96]={0};
and access/set like this::
for(row=0;row<12;row++) //to point to the row (0-11 rows)
{
rcvd_data= recv_function(); //whatever your recv function is
for(col=0;col<96;col++) //to point to the col in that row (0-95 col)
{
*(image + row*96 + col)= rcvd_data[col]//whatever value you want to assign
}
}
and to transfer all 96 bytes in one go:
for(row=0;row<12;row++) //to point to the row (0-11 rows)
{
rcvd_data= recv_function(); //whatever your recv function is
memcopy((image + row*96), rcvd_data, 96);
}
One thing I will add to this is be careful using just a char. Use something like unsigned char,signed char, or uint8_t when dealing with pure byte data. While a char is one byte I've seen loss of data because of it's use.
Raw arrays are a bit annoying. Use std::array instead:
using std::array;
array<byte,6> a = {B11111111, B10010000, B10011000, B10010100, B10010010, B11110001};
array<byte,6> b = {B11111111, B10000001, B10000001, B10000001, B10000001, B11111111};
array<array<byte,6>,2> c = {a, b};
std::array was introduced in c++11
You could do it as follows:
byte a[] = {B11111111, B10010000, B10011000, B10010100, B10010010, B11110001};
byte b[] = {B11111111, B10000001, B10000001, B10000001, B10000001, B11111111};
byte* c[2] = {a,b};
But it would be cleaner to just do a multidimensional array directly:
byte c[2][6] = {
{B11111111, B10010000, B10011000, B10010100, B10010010, B11110001},
{B11111111, B10000001, B10000001, B10000001, B10000001, B11111111}
};
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/
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!