pointer initialization

R

risha

hello,
Need to know how the pointer can be initailzed to a particular
location so that the value can be checked.

void FlashWrite1(volatile unsigned short *addr1,volatile unsigned short
*addr11,unsigned short ucVal )
{

unsigned short volatile *addr2;
(*addr2)=(0x7F12);
/* i get a warning that addr2 may not be initialized. I have tried
different ways but it is not happening. here addr2 needs to be initailzed
to 0x7f12 and the value passed by ucVal should be written into that
location
//&addr2=0x7F12;
addr2=&ucVal;
*addr2=ucVal;

*addr1=*addr2;/* writing into required address*/
*addr11=*addr2;/*writing into int flash*/

}
 
S

sachin

Hi Risha,
First I would like to point that volatile unsigned short is not the
same as unsigned short volatile.

Second, if you have to fill the value of the pointer addr2 to 0x&F12,
then it should be done like this
addr2 = 0x7F12;
/* This is wrong, *addr2 = 0x7F12; */

*addr2 = ucval;

The rest of the statements are not specific to this function I think
and could have been kept outside this function, may be in the calling
function itself.

Regards
Sachin
 
K

Krishanu Debnath

[top posting fixed]
risha wrote:



Hi Risha,
First I would like to point that volatile unsigned short is not the
same as unsigned short volatile.
Really? How?

Krishanu
 
M

manoj1978

volatile int * p and int * volatile p are different. but volatile int i
and int volatile i are same.
 
R

Richard Bos

[ Do not top-post. Corrected. ]

Then you need to assign a value _to_ addr2, not to the object it points
at. The warning is correct: at this moment, addr2 probably does not
point at any object, and your dereference causes undefined behaviour.
First I would like to point that volatile unsigned short is not the
same as unsigned short volatile.

Yes, it is.
Second, if you have to fill the value of the pointer addr2 to 0x&F12,
then it should be done like this
addr2 = 0x7F12;

Nope. This is one of the few occasions in C where you _should_ use a
cast. In fact, it's the one occasion where the cast actually makes
sense. You're assigning something that is not a pointer value to a
pointer object. This is seriously weird. In this case, it's _correctly_
seriously weird, but you need to tell the compiler that yes, you do know
what you're doing, and yes, you do want the type system to be
circumvented just this once. For that, you need the cast:

addr2 = (unsigned short volatile *)0x7F12;

AFAICT it's OK to leave out the volatile in the cast, which saves some
typing, but it's probably less head-scratch-provoking to leave it in.

Richard
 
C

Chris Torek

[snippage]

Then you need to assign a value _to_ addr2 ...


sachin said:
First I would like to point that volatile unsigned short is not the
same as unsigned short volatile.

Yes, it is.

Indeed, one can write this as:

short volatile unsigned *addr2;

or:

volatile short unsigned *addr2;

or:

unsigned volatile short *addr2;

or any other number of combinations. I happen to prefer the order
"volatile unsigned short" myself, but this is a matter of taste rather
than correctness.
... This is one of the few occasions in C where you _should_ use a
cast. ...
addr2 = (unsigned short volatile *)0x7F12;
AFAICT it's OK to leave out the volatile in the cast, which saves some
typing, but it's probably less head-scratch-provoking to leave it in.

It is OK but, as you did, I would include it. Morever, I would
probably do at least this, if not something even fancier (depending
on the ultimate application of the program):

#define FLASH_DEVICE_ADDR ((volatile unsigned short *)0x7f12)
...
volatile unsigned short *hw = FLASH_DEVICE_ADDR;
...
*hw = SOME_MACRO; /* tell it to do X */
error = flash_wait(hw); /* wait for response */
if (error) ...
*hw = SOME_OTHER_MACRO; /* tell it to do Y */
error = flash_wait(hw); /* wait for response */
if (error) ...

Given the #define, the local variable is not actually required:

*FLASH_DEVICE_ADDR = SOME_MACRO;

will have the same effect. (But I would use the local variable
here anyway. I would probably not name it just "hw" though --
depending on what kind of hardware register it is, it would get
an appropriate name reflecting that.)​
 
R

risha

addr2 = (unsigned short volatile *)0x7F12;
Thank u very much for the help extended to one and all, specially
richard sir i am grateful the statement above worked the way i
wanted.I am a student and help like this makes us get back to our
toes.

thanks again,
Risha
 
C

CBFalconer

risha said:
addr2 = (unsigned short volatile *)0x7F12;
Thank u very much for the help extended to one and all, specially
richard sir i am grateful the statement above worked the way i
wanted.I am a student and help like this makes us get back to our
toes.

What statement above? You failed to quote anything, and I suspect
you changed the subject line again. Also the use of abbreviations
such as 'u' serve only to annoy and make things hard to read. A
sentence terminating '.' should be followed by at least one blank.
See my sig below if you insist on using the broken google groups
posting mechanism.
 
C

CBFalconer

risha said:
addr2 = (unsigned short volatile *)0x7F12;
Thank u very much for the help extended to one and all, specially
richard sir i am grateful the statement above worked the way i
wanted.I am a student and help like this makes us get back to our
toes.

What statement above? You failed to quote anything, and I suspect
you changed the subject line again. Also the use of abbreviations
such as 'u' serve only to annoy and make things hard to read. A
sentence terminating '.' should be followed by at least one blank.
See my sig below if you insist on using the broken google groups
posting mechanism.
 
R

Richard Harter

What statement above? You failed to quote anything, and I suspect
you changed the subject line again. Also the use of abbreviations
such as 'u' serve only to annoy and make things hard to read. A
sentence terminating '.' should be followed by at least one blank.
See my sig below if you insist on using the broken google groups
posting mechanism.

This is the statement he was referring to:
addr2 = (unsigned short volatile *)0x7F12;



Richard Harter, (e-mail address removed)
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
 
C

CBFalconer

Richard said:
This is the statement he was referring to:
addr2 = (unsigned short volatile *)0x7F12;

The point is not so much to get the reference, but to make the OP
realize that his posts are useless without proper quoting, and that
a means to that end is available.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,430
Latest member
7dog123

Latest Threads

Top