![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Guest
|
Function and function call? [Solved] |
Posted: Sun Sep 06, 2009 2:42 am |
|
|
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:-) |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
Function Pointer fali. |
Posted: Sun Sep 06, 2009 2:52 am |
|
|
Hi
Sorry I found the solution:
If I use this in the function it work:-)
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Sun Sep 06, 2009 4:02 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|