![]() |
| | #1 |
| Registered User Join Date: Apr 2007
Posts: 2
| Array of Pointers to Arrays Code: warning C4047: 'initializing' : 'const BYTE *' differs in levels of indirection from 'int' Here's the array declaration and a diagram of what I expect it to look like in memory, with red text for simplifications/commentary: Code: const BYTE *gc_ppbCharMatrices[ /* UCHAR_MAX + 1 */ ] =
{
/* Control characters */
NULL, NULL, /* 32 NULL entries total indicate no bitmap available */
/* Bitmap data */
{ 0x00, 0x00, 0x00, 0x00, 0x00 }, /* ' ' */
/* I expect this to initialize a constant array
somewhere else in memory and return the
pointer to that array. */
{ 0x00, 0x00, 0x2f, 0x00, 0x00 }, /* '!' */
{ 0x00, 0x03, 0x00, 0x03, 0x00 }, /* '"' */
{ 0x14, 0x7f, 0x14, 0x7f, 0x14 }, /* '#' */
/* ...Etc; includes bitmaps up to 126 ('~' character) */
NULL, NULL
/* ...NULL pointers continue from 127 through to the end of the array */
};
http://img266.imageshack.us/img266/9154/arrayur3.gif The contents of the array aren't important, but I need to be able to initialize the array in this way with pointers to other constant arrays. It gets stored in ROM and can't be altered after initialization. How can this be done in C, or is it even possible? |
| Biozero is offline | |
| | #2 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,706
| You have to give each row a name, as you can't create anonymous arrays. Eg. Code: const BYTE m_space[] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
etc
const BYTE *gc_ppbCharMatrices[ /* UCHAR_MAX + 1 */ ] =
{
/* Control characters */
NULL, NULL, /* 32 NULL entries total indicate no bitmap available */
/* Bitmap data */
m_space, /* ' ' */
};
Code: const BYTE letters[][5] = {
{ 0x00, 0x00, 0x00, 0x00, 0x00 }, // add rows for each letter
};
const BYTE *gc_ppbCharMatrices[ /* UCHAR_MAX + 1 */ ] =
{
/* Control characters */
NULL, NULL, /* 32 NULL entries total indicate no bitmap available */
/* Bitmap data */
letters[0], /* then letters[1] etc */
};
But consider making the whole thing a 2D array for simplicity Code: const BYTE gc_bCharMatrices[ /* UCHAR_MAX + 1 */ ][5] =
{
/* Control characters */
{ 0 }, /* 32 empty entries total indicate no bitmap available */
/* Bitmap data */
{ 0x00, 0x00, 0x00, 0x00, 0x00 }, /* ' ' */
{ 0x00, 0x00, 0x2f, 0x00, 0x00 }, /* '!' */
{ 0x00, 0x03, 0x00, 0x03, 0x00 }, /* '"' */
{ 0x14, 0x7f, 0x14, 0x7f, 0x14 }, /* '#' */
/* ...Etc; includes bitmaps up to 126 ('~' character) */
};
Compare with your approach, which is 256*4 pointers, and 96*5 bitmap data = 1504 bytes. |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Apr 2007
Posts: 2
| Although I wish I didn't have to hard code the array indexes, your second solution will work fine. As for the memory savings, pointers are only two bytes on the hardware I'm using. It ends up being 992 bytes total before I cut out any unnecessary characters. Thanks for the help! |
| Biozero is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pointers and multi dimensional arrays | andrea72 | C++ Programming | 5 | 01-23-2007 04:49 PM |
| Array coping into another array?or function returning array | baniakjr | C++ Programming | 6 | 11-09-2006 03:28 AM |
| 2D Array of Pointers | Slavakion | C++ Programming | 12 | 03-31-2004 05:05 PM |
| Searching an array with pointers | pxleyes | C Programming | 16 | 03-23-2004 05:07 PM |
| Passing pointers between functions | heygirls_uk | C Programming | 5 | 01-09-2004 06:58 PM |