|
|
View previous topic :: View next topic |
Author |
Message |
mds
Joined: 06 Nov 2005 Posts: 29
|
sizeof element in multi dimensional array |
Posted: Sun Nov 06, 2005 10:30 pm |
|
|
hi all
maybe doing something out of the ordinary here. I have a 2 dimensional array called pix map. the current contains only 3 but this will be expanded as new ones are added to the code.
so rather than write hard coded numbers in the prog. I wanted to use sizeof to find the sizes of the indexs in the array. My test print was
printf("%u,%u",sizeof(pix_map),sizeof(pix_map[1])
my results:
pix_map= 21 understandable but i expected 3
pix_map[1] =1 ?? I was hoping to see a 7
how do i find out how many elements in the 1st and 2nd indexs?
Code: | const byte pix_map[3][7]=
{
//uparrow
{0b00000100,
0b00001110,
0b00010101,
0b00000100,
0b00000100,
0b00000100,
0b00000100},
//dwnarrow
{0b00000100,
0b00000100,
0b00000100,
0b00000100,
0b00010101,
0b00001110,
0b00000100},
// box_on
{0b00000000,
0b00011111,
0b00011111,
0b00011111,
0b00011111,
0b00011111,
0b00011111}
};
|
Code: | for(i=0;i<sizeof(pix_map);i++) //should be 3
{
for(l=0;l<pix_map[1];l++) //should be 7
{
fprintf(pc,"%X\n\r",pix_map[i][l]); ///test print--remove
lcd_send_byte(1,pix_map[i][l]); // write to CG ram area
}
fprintf(pc,"\n\r"); //test print --remove
}
|
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sun Nov 06, 2005 10:48 pm |
|
|
Just use a #define instead of a number.
Code: |
#define MAX_ELEMENTS 7 |
|
|
|
mds
Joined: 06 Nov 2005 Posts: 29
|
|
Posted: Mon Nov 07, 2005 3:34 pm |
|
|
Quote: | Just use a #define instead of a number.
Code:
#define MAX_ELEMENTS 7 |
short n sweet thanks but not what i was after. I use that solution all the time.
I was hoping to learn that sizeof could better solve this situation for me.
Are you saying that sizeof is incapable of being used to find the size of the elements like this? |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Nov 07, 2005 3:36 pm |
|
|
mds wrote: | Quote: | Just use a #define instead of a number.
Code:
#define MAX_ELEMENTS 7 |
short n sweet thanks but not what i was after. I use that solution all the time.
I was hoping to learn that sizeof could better solve this situation for me.
Are you saying that sizeof is incapable of being used to find the size of the elements like this? |
Gives the number of bytes taken up by the array. Won't tell you anything about the number of elements. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Nov 07, 2005 4:53 pm |
|
|
In order to make the sizeof operator work you can create a struct or typedef for the second dimension of your array.
Code: | typedef byte picture[7];
const picture pix_map[3]=
{
//uparrow
{0b00000100,
0b00001110,
0b00010101,
0b00000100,
0b00000100,
0b00000100,
0b00000100},
//dwnarrow
{0b00000100,
0b00000100,
0b00000100,
0b00000100,
0b00010101,
0b00001110,
0b00000100},
// box_on
{0b00000000,
0b00011111,
0b00011111,
0b00011111,
0b00011111,
0b00011111,
0b00011111}
};
#define PICTURE_SIZE (sizeof(picture))
#define NR_OF_PICS (sizeof(pix_map) / PICTURE_SIZE)
|
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Nov 07, 2005 6:08 pm |
|
|
I took it that either array dimension may change. |
|
|
mds
Joined: 06 Nov 2005 Posts: 29
|
|
Posted: Mon Nov 07, 2005 11:56 pm |
|
|
thanks for clarifying the ins and outs of sizeof.
ill try both schemes and see which suits best |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|