2

Hi i am having 2 questions here

  1. How do i store a hex value in a buffer, say for example 0x0a and 0x1F;

    char buffer[2] = "0x0a 0x1F";
    

    But this is not right method, It is giving size 10 instead of 2. Can any one suggest how can i proceed.

  2. I have seen the array like this

    char buffer[] = " static array";
    

    In the structure also,

    struct Point {
       char x[];
       char y[];
    };
    

    what does it mean? how much size it will take for compilation

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
user3095102
  • 29
  • 1
  • 4

1 Answers1

5

For the first, assuming you really want a two-byte array rather than a three-byte string (including NULL terminator), you can use:

char buffer[] = {0x0a, 0x1f};

For the second, the easiest way to find out is to simply check:

sizeof(buffer)

or:

sizeof(struct Point)

although I'm pretty certain your structure definition will fail because char x[] is not a complete type. Current versions of the standard allow flexible array sizes at the end of structures but not the way you have it there.

Likely sizes of the two (once you declare struct Point with char x[5]) will be 14 (number of characters in " static array" including the NULL terminator) and 5 (the size of x itself (flexible array members tend not to take up space, they're more for allowing arbitrary extra space if the memory block is obtained by malloc, for example).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953