908GP32 - Address issue - pointer in C

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

908GP32 - Address issue - pointer in C

1,531 Views
Stefano_RadioLa
Contributor I
Hi,
I am programmind 908GP32 with codewarrior usign C.
I have this question: i have a variable ( int not a pointer) and a data structure (array of int)
How can I do the following operation?

myvariable=address data structure

I need this because I am trying to implent a eeprom using flash with the driver descripted in an3040.      

Do you have some suggestion?

Tx

--
Alban Edit: Changed title: Part number must be shown and clarified subject.



Message Edited by Alban on 2007-06-07 11:36 AM
Labels (1)
0 Kudos
2 Replies

258 Views
thisobj
Contributor III
Hello Stefano,
You'll need to cast the address value to an "int" type to avoid a type mis-match error. 
The following should work:

int myvariable;

int somearray[2];

void main(void) {

myvariable = (int)(&somearray);

// now to use "myvariable" as a pointer....

*(  (int *)myvariable ) = 20;      // places "20" at somearray[0];

}

Note that using pointers that are built by casting can lead to very difficult-to-find bugs.  This usually occurs when incrementing or decrementing the pointer.

Frank


0 Kudos

258 Views
Alban
Senior Contributor II
Hello,

& is the operator for address:

myvariable = &DataStruct;


Cheers,
Alban.
0 Kudos