Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a byte array in C++?

Please have a look at the followng header file

#pragma once

class MissileLauncher
{
public:
    MissileLauncher(void);

private:
    byte abc[3];
};

This generated the error

Error   1   error C2143: syntax error : missing ';' before '*'  

I tried to do it in this way

byte *abc;

but it also failed, same error. However, I noticed I can call other built in tyes arrays in this way for an example, an int array. Why this is happening to byte array? How to solve this? I would like to assign the values in cpp file. Any ideas?

like image 920
Soldier Avatar asked May 10 '13 19:05

Soldier


People also ask

What is C byte array?

byte array in CAn unsigned char can contain a value from 0 to 255, which is the value of a byte. In this example, we are declaring 3 arrays – arr1, arr2, and arr3, arr1 is initialising with decimal elements, arr2 is initialising with octal numbers and arr3 is initialising with hexadecimal numbers.

What is byte in C?

A byte is typically 8 bits. C character data type requires one byte of storage. A file is a sequence of bytes. A size of the file is the number of bytes within the file. Although all files are a sequence of bytes,m files can be regarded as text files or binary files.

What is a byte stream in C?

In the byte stream model, a file is just a stream of bytes, with no record boundaries. Newline characters written to the stream appear in the external file. If the file is opened in binary mode, any newline characters previously written to the file are visible on input.

What is the byte size of array?

If we want to determine the size of array, means how many elements present in the array, we have to write calculation with the help of sizeof() operator. Sizeof(arr[]) / sizeof(arr[0]) ; Here, the size of arr[] is 5 and each float takes memory 8 bytes. So, the total memory is consumed = (5 * 8) bytes.


2 Answers

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;

like image 104
Michael Price Avatar answered Oct 01 '22 18:10

Michael Price


If you want exactly one byte, uint8_t defined in cstdint would be the most expressive.

http://www.cplusplus.com/reference/cstdint/

like image 21
TractorPulledPork Avatar answered Oct 01 '22 19:10

TractorPulledPork