View previous topic :: View next topic |
Author |
Message |
DonWare
Joined: 18 Jan 2006 Posts: 43
|
Pointer Problem |
Posted: Tue Jun 08, 2010 10:00 am |
|
|
I'm not sure this is related to my other problem I've been posting about
but I'm having a problem using a pointer. I was wondering if somebody could look at this code and see what might be wrong with it. The second comment describes the problem. Thanks.
Code: |
void main(){
int8 rx_buff[22];
rx_buff[3]=0x36;
called_routine(&rx_buff[3]); // I do need [3] here
}
void called_routine(int8 *string_start){
int8 cc;
cc=&(*string_start++); // cc = 6 at this point, not 0x36
}
|
|
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
Re: Pointer Problem |
Posted: Tue Jun 08, 2010 11:20 am |
|
|
DonWare wrote: | I'm not sure this is related to my other problem I've been posting about
but I'm having a problem using a pointer. I was wondering if somebody could look at this code and see what might be wrong with it. The second comment describes the problem. Thanks.
Code: |
void main(){
int8 rx_buff[22];
rx_buff[3]=0x36;
called_routine(&rx_buff[3]); // I do need [3] here
}
void called_routine(int8 *string_start){
int8 cc;
cc=&(*string_start++); // cc = 6 at this point, not 0x36
}
|
|
So you passed the address of rx_buffer[3] to call_routine(). Now *string_start = 0x36, and *string_start++ = 0x37. cc is still just the address of rx_buffer[3]. What do you think is wrong ? |
|
|
DonWare
Joined: 18 Jan 2006 Posts: 43
|
|
Posted: Tue Jun 08, 2010 11:34 am |
|
|
OK I figured it out.
It should be: cc=*string_start++;
Thanks. |
|
|
sjb
Joined: 13 Apr 2010 Posts: 34 Location: UK
|
Re: Pointer Problem |
Posted: Wed Jun 09, 2010 1:58 am |
|
|
mkuang wrote: | ...Now *string_start = 0x36, and *string_start++ = 0x37. cc is still just the address of rx_buffer[3]. What do you think is wrong ? |
NO! Just to avoid this little bit of dis-information confusing other people with pointer problems I think this need comment. The first part is correct, *string_start results in 0x36, but *string_start++ is still 0x36. The ++ operator increments string_start after the value is obtained for the *string_start part of the expression, it does not increment the 0x36 value. It's the same thing as writing...
Code: | cc = *string_start; // get the value pointed at by string_start
string_start++; // increments string_start
|
Note that string_start is an address, a pointer to a int8. Both the * and ++ operators work on the address but operator precedence means * works before ++ |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
Re: Pointer Problem |
Posted: Wed Jun 09, 2010 7:15 am |
|
|
sjb wrote: | mkuang wrote: | ...Now *string_start = 0x36, and *string_start++ = 0x37. cc is still just the address of rx_buffer[3]. What do you think is wrong ? |
NO! Just to avoid this little bit of dis-information confusing other people with pointer problems I think this need comment. The first part is correct, *string_start results in 0x36, but *string_start++ is still 0x36. The ++ operator increments string_start after the value is obtained for the *string_start part of the expression, it does not increment the 0x36 value. It's the same thing as writing...
Code: | cc = *string_start; // get the value pointed at by string_start
string_start++; // increments string_start
|
Note that string_start is an address, a pointer to a int8. Both the * and ++ operators work on the address but operator precedence means * works before ++ |
You are right. I stand corrected. |
|
|
|