IberoMedia
IberoMedia

Reputation: 2304

How to create a data type of a given size

I would like to create a struct of size 508 bytes.

Viewing this code I inferred I could create a struct of any size I want.

#include <stdint.h>

/**
 * Common Data Types 
 *
 * The data types in this section are essentially aliases for C/C++ 
 * primitive data types.
 *
 * Adapted from http://msdn.microsoft.com/en-us/library/cc230309(PROT.10).aspx.
 * See http://en.wikipedia.org/wiki/Stdint.h for more on stdint.h.
 */
typedef uint8_t  BYTE;
typedef uint32_t DWORD;
typedef int32_t  LONG;
typedef uint16_t WORD;

/**
 * BITMAPFILEHEADER
 *
 * The BITMAPFILEHEADER structure contains information about the type, size,
 * and layout of a file that contains a DIB [device-independent bitmap].
 *
 * Adapted from http://msdn.microsoft.com/en-us/library/dd183374(VS.85).aspx.
 */
typedef struct 
{ 
    WORD   bfType; 
    DWORD  bfSize;  
    WORD   bfReserved1; 
    WORD   bfReserved2; 
    DWORD  bfOffBits; 
} __attribute__((__packed__)) 
BITMAPFILEHEADER;

I need type int of size 508 bytes, and after I read this post: Size of the given structure I imagined I could create the int of the size I need. So I tried to create a struct like so:

typedef struct 
{ 
    int datachunk : 4064; 
}  
BLOCK;

Of course, that is wild imagination and I am wrong.

So, how can I create a struct of size 508 bytes that contains a data type integer of the same size (i.e. 4064?

Upvotes: 0

Views: 1580

Answers (2)

Paul Hankin
Paul Hankin

Reputation: 58221

A bit-field can't be wider than an int.

6.7.2.1.3 The expression that specifies the width of a bit-field shall be an integer constant expression with a nonnegative value that does not exceed the width of an object of the type that would be specified were the colon and expression omitted. If the value is zero, the declaration shall have no declarator.

Upvotes: 1

user529758
user529758

Reputation:

Here's how:

typedef unsigned char fivehundred_and_eight_bytes[508];

Upvotes: 3

Related Questions