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 CCS Technical Support

passing pointers question

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



Joined: 30 Sep 2010
Posts: 38

View user's profile Send private message

passing pointers question
PostPosted: Sat Feb 20, 2016 6:49 pm     Reply with quote

Hello All,

I have constructed a simple function:

Code:
 
void dec_to_2digit(int16 *tensdigit, int16 *onesdigit, int8 dec_number){
 
  if (dec_number >= 10){
      for (*tensdigit = 0; dec_number >= 10; dec_number -= 10) *tensdigit++;
  }
   
  else *tensdigit = 0;
  *onesdigit = dec_number;

//even tried this -> *tensdigit = 3;
   
  return;
  }


when I call it using :

Code:


dec_to_2digit(&tens, &ones, 65);

printf("tens %u, ones %u\n\r", tens, ones);


ones is 5,
tens is Always 0.

I even tried forcing a value into *tensdigit, and It reads back 0.

ver 5.045

Any help is very appreciated.
Davidd
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Feb 20, 2016 10:28 pm     Reply with quote

Fix it by adding parentheses as shown in bold below:
Quote:
if (dec_number >= 10){
for (*tensdigit = 0; dec_number >= 10; dec_number -= 10) (*tensdigit)++; }

Then you will get this output:
Quote:
tens 6, ones 5


Here is a lengthy forum post that goes into more detail on this topic
(Order of operations when dereferencing a pointer):
http://stackoverflow.com/questions/859770/on-a-dereferenced-pointer-in-c
davidd



Joined: 30 Sep 2010
Posts: 38

View user's profile Send private message

PostPosted: Sun Feb 21, 2016 6:43 am     Reply with quote

Thanks PCM Programmer!
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