CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Pointer Problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
DonWare



Joined: 18 Jan 2006
Posts: 43

View user's profile Send private message

Pointer Problem
PostPosted: Tue Jun 08, 2010 10:00 am     Reply with quote

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

View user's profile Send private message Send e-mail

Re: Pointer Problem
PostPosted: Tue Jun 08, 2010 11:20 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jun 08, 2010 11:34 am     Reply with quote

OK I figured it out.

It should be: cc=*string_start++;

Thanks.
sjb



Joined: 13 Apr 2010
Posts: 34
Location: UK

View user's profile Send private message

Re: Pointer Problem
PostPosted: Wed Jun 09, 2010 1:58 am     Reply with quote

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

View user's profile Send private message Send e-mail

Re: Pointer Problem
PostPosted: Wed Jun 09, 2010 7:15 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group