C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-19-2007, 01:11 PM   #1
Registered User
 
Join Date: Apr 2007
Posts: 2
Array of Pointers to Arrays

I'm trying to store an array of pointers to arrays, but I can't get the declaration syntax right. My compiler (MS Visual Studio 2005 Professional Edition) gives me this error message:
Code:
warning C4047: 'initializing' : 'const BYTE *' differs in levels of indirection from 'int'
The goal of this array is to store images of characters that I draw to LED matrices. I store each image as an array of 5 BYTEs (unsigned chars). Not every character from 0 to 255 has an image, so I want to store a NULL pointer for characters that don't have images.

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 */
};
Diagram of intended memory layout:
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   Reply With Quote
Old 04-19-2007, 02:10 PM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
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, /* ' ' */
};
Though you might be able to do this
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) */
};
256 * 5 = 1280 bytes.

Compare with your approach, which is 256*4 pointers, and 96*5 bitmap data = 1504 bytes.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 04-19-2007, 02:31 PM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:33 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22