CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Question about using a pointer to a structure

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
naughty_mark



Joined: 29 Aug 2012
Posts: 97

View user's profile Send private message

Question about using a pointer to a structure
PostPosted: Thu Jun 12, 2014 12:28 am     Reply with quote

Hi guys,

I know the question must be silly and I always get confused when I play with pointer (In fact, the pointer play with me!). I searched this forum and didn't get my answer.
I am using dsPIC30F6014A with CCS compiler version 5.016.

The whole story is I try to define several font structure and use a pointer to switch among different fonts when necessary.
The font structure is defined like
Code:

typedef struct _charLib  //for character library
{
   unsigned int charAmount;
   unsigned int width;
   unsigned int heigth;
   unsigned int bytePerRow;
   unsigned char *pCharArray;
   _fptr addColFunPtr;
} charLib;

Don't worry about _ptr addColFunPtr, it just a pointer to a function and not the key of my question.
The question is about "unsigned char *pCharArray", it attempts a pointer a char array which record the bitmap of characters, the char array is defined as
Code:

rom unsigned char Font1616Array[FONT1616_TOTAL_CHAR][CHAR_DOUBLE_WIDTH] =
{
   /* Character Data - Index: 0 */
   0x00,0x00,0x00,0x00,0xF0,0x3F,0x10,0x20,0x10,0x20,0x10,0x20,0x10,0x20,0x10,0x20,0x10,0x20,0x10,0x20,0x10,0x20,0xF0,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

   /* Character Data - Index: 1 */
   0x00,0x00,0x00,0x00,0xF0,0x3F,0x10,0x20,0x10,0x20,0x10,0x20,0x10,0x20,0x10,0x20,0x10,0x20,0x10,0x20,0x10,0x20,0xF0,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
 ....
....
...
};


I define a font structure entity as
Code:

charLib Font16x16 = {
      FONT1616_TOTAL_CHAR,
      16,
      16,
      2,
      &Font1616Array,
      &addCurColFont1616,
};


My pointer to font structure is
Code:

static charLib *currentFont;


and I wrote
Code:

currentFont = &Font16x16;


Now my silly question is:
what expression or statement I should use to grab the value in fontArray? such as I want to grab the second element in that array.

I tried many ways and use printf to debug, but the output is weird, and I got stuck with pointer to structure which contains another pointer to char array.

Thanks for your time and help. I do appreciate.

Kind Regards
Mark

EDIT:
another thing I found is, by using "ROM" rather than "const" is because I want to use a pointer to point a big array which store in ROM but not RAM, and I tried to
Code:

printf("add = %Lu\r\n", &Font1616Array);

The output is add = 0.....Font1616Array is in 0542AA-0542A9 as it is showed in .sym file. It sounds using "ROM" can only make that variable be pointed, but the address can not be obtained, am I right?
Anyway, I am not insist on my current method, if you have better solution, it is welcomed.
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Thu Jun 12, 2014 12:57 am     Reply with quote

Key is in the understanding that there are two address spaces.

Hence the compiler has to _know_ that a pointer to rom, _is_ a pointer to rom....

So it allows a pointer to be declared as:

unsigned char rom *pCharArray;

which says this is a pointer to an unsigned char in rom.

Now, 'depending on compiler version', this works, and the compiler then knows that the pointer is to rom, and correctly switches to addressing the other memory space, when this is used.

Try it. Smile

Best Wishes
naughty_mark



Joined: 29 Aug 2012
Posts: 97

View user's profile Send private message

PostPosted: Thu Jun 12, 2014 4:37 pm     Reply with quote

Ttelmah wrote:
Key is in the understanding that there are two address spaces.

Hence the compiler has to _know_ that a pointer to rom, _is_ a pointer to rom....

So it allows a pointer to be declared as:

unsigned char rom *pCharArray;

which says this is a pointer to an unsigned char in rom.

Now, 'depending on compiler version', this works, and the compiler then knows that the pointer is to rom, and correctly switches to addressing the other memory space, when this is used.

Try it. Smile

Best Wishes

Hi, Ttelmah
Thanks for your kind help. I just had a try as you instructed, change my font structure definition as
Code:

typedef struct _charLib  //for character library
{
   unsigned int charAmount;
   unsigned int width;
   unsigned int heigth;
   unsigned int bytePerRow;
   unsigned char rom *pCharArray;
   _fptr addColFunPtr;
} charLib;

and my 16*16 font entity as
Code:

charLib Font16x16 = {
      FONT1616_TOTAL_CHAR,
      16,
      16,
      2,
      &Font1616Array,
      &addCurColFont1616,
};

then I use printf to output the address of Font1616Array
Code:

currentFont = &Font16x16;
printf("address = %Lu\r\n",currentFont->pCharArray);

The output is still 0 Sad no idea if I did something wrong, and my compiler version is v5.016.
The other method to work around this problem I can get is write different "printString()" function for each font, and in the charLib structure add another function pointer points to this printString function rather than use the same printString function and deal with different font array by using rom pointer Rolling Eyes I may try it later.
If you have any suggestion about "rom pointer" you mentioned, please let me know, even I can work around this, I am still interested in make this question cleared. Smile
Idea or suggestion from Anyone else are welcomed Very Happy
Thanks for your time Ttelmah again.

Kind Regards
Mark
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Thu Jun 12, 2014 11:58 pm     Reply with quote

OK.

I don't think you can declare the pointers at compile time.
Historically, CCS didn't support this at all.

Then with V5, they started allowing it for 'variables', so an array name could be put as an entry in a structure.

However I've never tried it with constant arrays.

I've always added the few extra lines, to actually write the address into the pointer, since it didn't work in the past. So

Font16x16.pCharArray=Font1616Array;

You don't need the '&', since an array name is the pointer.

Historically also, CCS does tend to not accept things a layer or more down in definitions, that it accepts at the 'top'. So things that it accepts in a simple declaration, tend to go wrong, once you are inside a structure for example...
naughty_mark



Joined: 29 Aug 2012
Posts: 97

View user's profile Send private message

PostPosted: Sun Jun 15, 2014 4:14 pm     Reply with quote

Ttelmah wrote:
OK.

I don't think you can declare the pointers at compile time.
Historically, CCS didn't support this at all.

Then with V5, they started allowing it for 'variables', so an array name could be put as an entry in a structure.

However I've never tried it with constant arrays.

I've always added the few extra lines, to actually write the address into the pointer, since it didn't work in the past. So

Font16x16.pCharArray=Font1616Array;

You don't need the '&', since an array name is the pointer.

Historically also, CCS does tend to not accept things a layer or more down in definitions, that it accepts at the 'top'. So things that it accepts in a simple declaration, tend to go wrong, once you are inside a structure for example...

Thanks for your help, Ttelmah. I do appreciate.
I may try some other way to work around this.

Kind Regards
Mark
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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