Translate but can not C

H

Henning

Hi group!
I'm translating a prog written in IAR C for AVR.
But my knowledge in C is too low to understand this.

//==========================================================================
===
// Sets or clears the bit 'index' in the bit table 'table', regarding the
value
// of 'value'.

void putBit(unsigned char *table, unsigned char index, unsigned char value)
{
unsigned char *p = &table[index >> 3];
unsigned char m = 0x80 >> (index & 0x07);

*p = (value) ? (m | *p) : (~m & *p);
}

What does *p = (value) ? ...... do?
tia/ Henning
 
M

Malcolm

Henning said:
void putBit(unsigned char *table, unsigned char index, unsigned char value)
{
unsigned char *p = &table[index >> 3];
unsigned char m = 0x80 >> (index & 0x07);

*p = (value) ? (m | *p) : (~m & *p);
}

What does *p = (value) ? ...... do?
*p - take the memory location pointed to by p
= - write to it
(value) ? x : y - if value is non-zero write x, else write y
m | *p - logical OR of m and *p
~m & *p - logical AND of complement of m and *p

the ? : syntax is just a terse if ... else.
 
A

Alex Fraser

Henning said:
I'm translating a prog written in IAR C for AVR.
But my knowledge in C is too low to understand this.

//==========================================================================
===
// Sets or clears the bit 'index' in the bit table 'table', regarding the
value
// of 'value'.

void putBit(unsigned char *table, unsigned char index, unsigned char
value)
{
unsigned char *p = &table[index >> 3];
unsigned char m = 0x80 >> (index & 0x07);

*p = (value) ? (m | *p) : (~m & *p);
}

What does *p = (value) ? ...... do?

This:
expr1 = expr2 ? expr3 : expr4;
Is equivalent to this:
if (expr2) expr1 = expr3; else expr1 = expr4;

Personally, I would have written the above as (IMO more idiomatic):
if (value) *p |= m; else *p &= ~m;

Alex
 
H

Henning

Malcolm said:
Henning said:
void putBit(unsigned char *table, unsigned char index, unsigned char value)
{
unsigned char *p = &table[index >> 3];
unsigned char m = 0x80 >> (index & 0x07);

*p = (value) ? (m | *p) : (~m & *p);
}

What does *p = (value) ? ...... do?
*p - take the memory location pointed to by p
= - write to it
(value) ? x : y - if value is non-zero write x, else write y
m | *p - logical OR of m and *p
~m & *p - logical AND of complement of m and *p

the ? : syntax is just a terse if ... else.
Thanx a lot! might struggle into more q's later.
/Henning
 
M

Mark F. Haigh

Henning said:
But my knowledge in C is too low to understand this.
What does *p = (value) ? ...... do?

Let's see... You obviously don't know C, and the people here aren't
of the disposition to teach it to you.

So get yourself a book on C (see FAQ question 18.10), and read it.


"Build a man a fire, and he's warm for a day. Set a man on fire, and
he's warm for the rest of his life." - StarChaser, Sep 04 2000


Mark F. Haigh
 
A

Allin Cottrell

Mark said:
Let's see... You obviously don't know C, and the people here aren't
of the disposition to teach it to you.

A little abrupt. People here are very willing to help a C novice
advance his or her understanding -- given evidence that the novice
has put in a modicum of effort. I'm not sure the condition is
satisfied in this case, but:

*p means (roughly) "the content of p", where p is implicitly a
pointer to some sort of object. So, for example, if p is a
pointer-to-int, then

*p = 3;

says "store the integer value 3 at the memory address indicated by
the pointer variable p."
So get yourself a book on C (see FAQ question 18.10), and read it.

Good advice.

Allin Cottrell.
 
K

Keith Thompson

Allin Cottrell said:
*p means (roughly) "the content of p", where p is implicitly a
pointer to some sort of object.

I think "the content of p" is a poor choice of words. Normally that
would refer to the value of the variable p, which is a pointer value.

*p refers to the object to which p points.
 

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
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top