2

I want to define a type that be equal to an array of 16 byte. Something such as this:

typedef uint8_t[16] mynewType;

but I am getting error. How can I define such type?

I am getting several errors on this line such as:

missing ';' before '['  
empty attribute block is not allowed    
missing ']' before 'constant'
'constant'  
mans
  • 17,104
  • 45
  • 172
  • 321
  • Could do `using mynewType = struct { char data[16]; };`. That will avoid the array degenerating into a pointer at a the drop of a hat. – Eljay May 22 '18 at 14:28
  • @Eljay But is does not guarantee the type is exactly 16 bytes wide. There could be padding or if `CHAR_BIT` is more than 8 then it would also be larger. – NathanOliver May 22 '18 at 14:31
  • 1
    @NathanOliver _"The sizeof operator yields the number of bytes occupied by a non-potentially-overlapping object of the type of its operand"_ and _"sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1"_, regardless of `CHAR_BIT` – Passer By May 22 '18 at 14:36
  • @PasserBy Thanks. At least now I know `CHAR_BIT` bits is a byte. – NathanOliver May 22 '18 at 14:39
  • @NathanOliver • one oddball platform I worked on, a byte (or char) was 13-bits. A word (or int) was 26-bits. – Eljay May 22 '18 at 16:26
  • @Eljay Interesting. Can you please share which platform is this? Is it a widely used platform or a special system? – mans May 23 '18 at 08:08
  • The platform is modern C++ (C++11 or later). Modern C++ is widely used. – Eljay May 23 '18 at 12:13
  • @Eljay I mean the platform that has char size of equal to 13 bits! Which processor supporting it? – mans May 23 '18 at 12:42
  • lol, oops, I thought you were referring to the other comment. The 13-bit platform was created as a virtual machine by a professor at my university. He would torture students by forcing them to use his 13-bit platform for his assignments, such as creating a compiler/linker or assembler, and the students had to get a ton of badly written documentation from Kinko's ($$$). I think he created his virtual machine in the 1960s, back when some machines had 6-bit bytes, others had 36-bit bytes; on a PDP-8 (a 12-bit machine). Wild and wooly times. – Eljay May 23 '18 at 13:37

5 Answers5

6

A typedef is like a declaration, but with an extra typedef in front.

So if

uint8_t my_array[16]; 

declares a new array.

typedef uint8_t my_array[16]; 

makes my_array the type of such an array.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
4

Just

typedef uint8_t mynewType [16];
Anton Malyshev
  • 8,686
  • 2
  • 27
  • 45
2

Like an array variable:

typedef uint8_t mynewType[16];
273K
  • 29,503
  • 10
  • 41
  • 64
2
typedef unsigned char mynewType [16];

is the portable way of allocating 16 bytes on any platform; CHAR_BIT does not necessarily have to be 8.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Is a byte 8 `CHAR_BIT` or is it just 8 bits? Do you still call a 16 bit char a byte on those types of sytems? – NathanOliver May 22 '18 at 14:35
  • Indeed I would. The term *octet* is used to describe exactly 8 bits. – Bathsheba May 22 '18 at 14:37
  • What do you think of this blog post? https://gustedt.wordpress.com/2010/06/01/how-many-bits-has-a-byte/ – Fred Larson May 22 '18 at 14:53
  • @FredLarson: That's the POSIX standard, which is not at the time of writing the same as the C++ standard. POSIX also states that a user defined variable ending with `_t` puts the entire program into an undefined state! – Bathsheba May 22 '18 at 14:57
  • Except that `uint8_t` *et al* are now part of C++11 and C99, not just POSIX. – Fred Larson May 22 '18 at 15:07
  • @FredLarson: I'm not sure about that. That's a question in itself. I'll keep an eye out and upvote it should you pose it. – Bathsheba May 22 '18 at 15:08
  • I might have been incorrect, reading more closely the comments discussion here: https://stackoverflow.com/a/3200969/10077 – Fred Larson May 22 '18 at 15:10
  • @FredLarson: Would still be a good question: makes a change to all the `i++ + ++i` trash we get these days. – Bathsheba May 22 '18 at 15:11
1

You can use a struct with an array field of that size. But you will still need to set the individual byte values. You can also use a union if you want to access the different memory chunks in different ways.

// simple data structure of 16 bytes
struct pack_16 {
    uint8_t data[16];
}
// sizeof(pack_16) == 16

// multi type access of 16 bytes
union multi_pack_16 {
    uint8_t  uint_8[16];
    uint16_t uint_16[8];
    uint32_t uint_32[4];
    uint64_t uint_64[2];
}
// sizeof(multi_pack_16) == 16

Also, depending on your compiler, the uint128_t data type may be defined which is 16 bytes in size.

flamewave000
  • 547
  • 5
  • 12