Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
// ignore - random bits just so I can compile
typedef unsigned char UBYTE;
typedef unsigned short UWORD;
#define _HOST
enum {
idx_GET_DEVICE_DESCRIPTOR,
idx_SET_ADDRESS,
STS_Success,
STS_ACK,
};
// These two structs go in a header file
struct tagdevRequest {
UBYTE devRequestIndex;
UWORD frequency;
#ifdef _HOST
UBYTE status;
UWORD RespLength;
UBYTE response[256];
#endif
};
struct foo {
struct tagdevRequest *array; // the * tells you its a pointer, that should be enough
size_t len;
};
// All these array initialisers go in say data.c
struct tagdevRequest devRequest1[] = {
// Get Device descriptor
{idx_GET_DEVICE_DESCRIPTOR, 1,
#ifdef _HOST
STS_Success,
0x12,
{0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xDC, 0x05, 0x00,
0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x01},
#endif
},
// Get Device descriptor
{idx_GET_DEVICE_DESCRIPTOR, 1,
#ifdef _HOST
STS_Success,
0x12,
{0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xDC, 0x05, 0x00,
0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x01},
#endif
},
// Get Device descriptor
{idx_GET_DEVICE_DESCRIPTOR, 1,
#ifdef _HOST
STS_Success,
0x12,
{0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xDC, 0x05, 0x00,
0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x01},
#endif
},
};
// Test # 2
struct tagdevRequest devRequest2[] = {
{idx_SET_ADDRESS, 1,
#ifdef _HOST
STS_ACK,
0x00,
{0x00},
#endif
},
};
// Test # 3
struct tagdevRequest devRequest3[] = {
// Get Device descriptor
{idx_GET_DEVICE_DESCRIPTOR, 1,
#ifdef _HOST
STS_Success,
0x12,
{0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xDC, 0x05, 0x00,
0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x01},
#endif
},
// Get Device descriptor
{idx_GET_DEVICE_DESCRIPTOR, 1,
#ifdef _HOST
STS_Success,
0x12,
{0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xDC, 0x05, 0x00,
0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x01},
#endif
},
// Get Device descriptor
{idx_GET_DEVICE_DESCRIPTOR, 1,
#ifdef _HOST
STS_Success,
0x12,
{0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xDC, 0x05, 0x00,
0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x01},
#endif
},
}; // End of all test Tables.
struct foo bar[] = {
#define MKENTRY(x) { x, sizeof(x)/sizeof(x[0]) }
MKENTRY(devRequest1),
MKENTRY(devRequest2),
MKENTRY(devRequest3),
};
size_t barlen = sizeof(bar)/sizeof(bar[0]);
// these are all you need to use the above from anywhere
extern struct foo bar[];
extern size_t barlen;
int main()
{
int i;
for (i = 0; i < barlen; i++) {
printf("Table %p contains %lu items\n", bar[i].array, bar[i].len);
}
return 0;
}
> Struct foo, first parameter is pointer to tagdevRequestPointer and second parameter is length