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

Function and function call? [Solved]

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








Function and function call? [Solved]
PostPosted: Sun Sep 06, 2009 2:42 am     Reply with quote

Hi

I want to pass a pointer to the var "pos" in the function call and then use the struct in the other function...

It is a small test program only for the problem. It look like I can't use the &pos in the function caller?

It compile but doesn't print the right things. Tested in "MPLAB"

Code:
typedef struct {
 int8 x,y;
} _pos;

_pos pos;

//param pxy is a pointer to pos.
void testpos1(_pos *pxy){
 int8 out;

 out=pxy.x; //not working
 printf("%u\n",out);
}

void testpos(){
 pos.x=10;
 pos.y=20;
 printf("pos.x:%u\n",pos.x);
 printf("pos.y:%u\n",pos.y);
 testpos1(&pos); //what is wrong here?
}

Hints:-)
Guest








Function Pointer fali.
PostPosted: Sun Sep 06, 2009 2:52 am     Reply with quote

Hi

Sorry I found the solution:
If I use this in the function it work:-)
Code:
out=pxy->x
Ttelmah
Guest







PostPosted: Sun Sep 06, 2009 4:02 am     Reply with quote

Glad you found it.

It is however perhaps worth 'thinking' about using one of the old 'help you' strategies to avoid this in future. It is common for people who regularly use pointer in functions etc., to deliberately give them a distinct 'class' of names. So (for example), reseve the letter 'p' as a first letter on your function names, and only give variables that are pointers a name beginning with this letter or use a longer indicater like 'ptr'. So if you called your variable in the function:

ptr_pxy

It becomes 'plain' that this is a pointer, and needs to be dereferenced to access the internal values, with:

ptr_pxy->x

or

*(ptr_pxy).x

This is just a useful 'strategy' to avoid this type of problem, especially when programs get larger...

Best Wishes
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