Searching \ for '[PIC] Pointer to array in ROM?' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'Pointer to array in ROM?'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] Pointer to array in ROM?'
2005\05\06@173743 by Harold Hallikainen

face picon face
I have an array of structures in ROM. I want a pointer in RAM to point to
that. The C manual that comes with MCC18 is a bit unclear on how to do
this. My array in ROM is defined as:

struct SongChord
 {
         unsigned char beats;
         unsigned int chord;
       };

#pragma romdata song1=0xe000

struct SongChord rom const Song1Array[]={
data goes here }


I'm trying to define a pointer that points to it. Thus far, I define the
pointer like this:

 struct SongChord rom const *SongArray;  // local pointer to a song

I'm trying to initialize the pointer with this:

             SongArray=&Song1Array;


and getting complaints from the compiler.

My goal is to have SongArray be a pointer in RAM that points to the array
in ROM so I can do something like this:

NumBeats=SongArray[3].beats;


Pointers in C have always been tricky to me. Mixing RAM and ROM makes it
worse!

Thanks for any help!

Harold


--
FCC Rules Updated Daily at http://www.hallikainen.com

2005\05\06@180611 by Dave Turner

picon face
Don't have a clue what's the problem, but others might find it helpful
if we know exactly what the compiler said.  A copy & paste of the
entire output might be helpful

On 5/6/05, Harold Hallikainen <spam_OUTharoldTakeThisOuTspamhallikainen.com> wrote:
{Quote hidden}

> -

2005\05\06@192150 by Harold Hallikainen

face picon face
Excellent idea! The compiler says:

C:\ProductDesigns\HuwaldtGuitar\Control\050415\main.c:369:Error [1131]
type mismatch in assignment

The erroneous line is:

SongArray=&Song1Array;

THANKS!

Harold


{Quote hidden}

>> --

2005\05\06@201824 by Kelly Kohls

picon face
Harold,

>I'm trying to define a pointer that points to it. Thus far, I define the
>pointer like this:

>  struct SongChord rom const *SongArray;  // local pointer to a song

Try this instead:

const struct SongChord rom *SongArray;  // local pointer to a song

I don't know if this will solve your problem, but here's the reference I
used:
http://publications.gbdirect.co.uk/c_book/chapter5/pointers.html


Kelly Kohls, N5TLE
http://www.qsl.net/n5tle
Bedford, Texas
There never enough time to do it right, but plenty of time to do it over.

2005\05\06@210446 by Paul Hutchinson

picon face
Here's a few things I'd try and or check, they might not succeed and then
I'd have to crack open the C references ;-).

Try accessing the array directly, no pointer:
NumBeats=Song1Array[3].beats;

IIRC, the name of an array is a pointer to the array so don't dereference
it:
SongArray=Song1Array;

See if the specific compiler supports pointers to structs in rom. I know one
68HC11 compiler I used a bit over a decade ago did not allow pointers to
structs.

Paul Hutch

> {Original Message removed}

2005\05\06@213347 by piclist

flavicon
face
On Fri, 6 May 2005, Harold Hallikainen wrote:

> struct SongChord rom const Song1Array[]={
> data goes here }
>
>  struct SongChord rom const *SongArray;  // local pointer to a song
>
>              SongArray=&Song1Array;
>
> and getting complaints from the compiler.

Don't use the address-of operator on an array.  The name of the array
decays to the address of the first element.  Just use:

SongArray = Song1Array;

--
John W. Temples, III

2005\05\06@220600 by Tad Anhalt

picon face
Harold Hallikainen wrote:
> C:\ProductDesigns\HuwaldtGuitar\Control\050415\main.c:369:Error
> [1131] type mismatch in assignment
[...]
> SongArray=&Song1Array;

>>> struct SongChord rom const Song1Array[]={ data goes here }
[...]
>>> struct SongChord rom const *SongArray;  // local pointer to a
>>> song
[...]
>>> SongArray=&Song1Array;

Song1Array is defined as an array of SongChord structs.

The subscript operator is just a clever(?) syntactical trick to hide the
pointer arithmetic behind arrays in c.

SongArray               =  &            Song1Array
(pointer to a SongArray) = (address of) (pointer to 1st SongArray)
       
Either of the following will work (choose the one that makes the most
sense to you):

 SongArray= Song1Array;      // direct pointer assignment
 SongArray= &Song1Array[0];  // address of dereferenced pointer

 You can also write the above statement like this ( the "+ 0" is to
help clarify the comparison).

 SongArray= & *(Song1Array + 0);

HTH,
Tad Anhalt

2005\05\07@123452 by Harold Hallikainen

face picon face
THANKS for all the responses! I used:

SongArray= Song1Array;      // direct pointer assignment

and it worked great.

THANKS!

Harold


{Quote hidden}

> -

2005\05\07@220611 by Bob Ammerman

picon face
When you refer to an array there is an implicit 'take the address of' (ie:
&) operator. So to assign the address of the first element of the array to
the pointer you would say:


SongArray = Song1Array;

rather than

SongArray = &Song1Array;

alternatively you could say:

SongArray = &Song1Array[0]

to get the same result.

Bob Ammerman
RAm Systems

{Original Message removed}

More... (looser matching)
- Last day of these posts
- In 2005 , 2006 only
- Today
- New search...