Array of Pointers to Arrays

This is a discussion on Array of Pointers to Arrays within the C Programming forums, part of the General Programming Boards category; I'm trying to store an array of pointers to arrays, but I can't get the declaration syntax right. My compiler ...

  1. #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?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  2. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  3. 2D Array of Pointers
    By Slavakion in forum C++ Programming
    Replies: 12
    Last Post: 03-31-2004, 04:05 PM
  4. Searching an array with pointers
    By pxleyes in forum C Programming
    Replies: 16
    Last Post: 03-23-2004, 04:07 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM

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