tags:

views:

506

answers:

5

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 du 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

+13  A: 

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
Adam Rosenfield
Note that you should use the "char foo[]" syntax in C++ as well
James Antill
+1  A: 

Doesn't

char myArray[] = {0x00, 0x01,0x02};

work?

Tom
+1  A: 

Try with:

char myArray[] = { 0x00, 0x11, 0x22 };
jbradaric
+1  A: 
none
+1  A: 

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.

quinmars