hardcode byte array in C

前端 未结 5 470
甜味超标
甜味超标 2021-02-02 11:06

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



        
相关标签:
5条回答
  • 2021-02-02 11:09
    • Array Initialization

    • Array Initialization

      char myArray[] = {0x00, 0x11, 0x22};

    0 讨论(0)
  • 2021-02-02 11:10

    Doesn't

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

    work?

    0 讨论(0)
  • 2021-02-02 11:15

    Try with:

    char myArray[] = { 0x00, 0x11, 0x22 };
    
    0 讨论(0)
  • 2021-02-02 11:25

    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
    
    0 讨论(0)
  • 2021-02-02 11:35

    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.

    0 讨论(0)
提交回复
热议问题