Re: Passing pointers?
- From: Robby <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 22 Dec 2007 07:54:00 -0800
Thankyou ajk for your post.
Note I have changed innitial value of z in main!
I have tried your suggestion and I know it is correct but the if statement
evaluates to true in the code below which is wrong. As you may already know,
I am using a special compiler from CCS and I have been having numerous
problems with it. And on top of that I haven't programmed in C for a while.
So put those two together and I find it quite an uphill battle. Right now I
cannot quit everything I am doing to become fully aquainted with C/C++ as
many would like including myself. So I am trying my best. I thank all for
your patients.
Here is the code I was taking about:
===========================================
void x(int *p)
{
y(p); /* p is the address of z, *p is the value 99 */
}
void y(int *p)
{
int x = *p; /* p is still address of z and *p value of z */
if(aGMM[i].VPIX_START == *p) //where aGMM[i].VPIX_START=255 and *p=17?
{other code gets executed ?????}
}
void main()
{
int z = 17; /* value of z is 99, address of z on the stack is &z */
x( &z );
}
==========================================
Back to CCS I go! :(
--
Best regards
Robert
"ajk" wrote:
On Fri, 21 Dec 2007 15:04:00 -0800, Robby.
<Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Hello,
I am wondering when passing a pointer to a function and require to further
pass it to another funtion (two functions deep), can we re-pass the pointer
to the second function using the "*" ?
Basically wrong values are displayed when dereferencing b in y function.
here is an example :
====================================
void x(int *a)
{
y(*a); //Is this correct??? I doubt it!
}
void y(int *b)
{
int x;
x=*b; //99 should be assigned to x.
}
void main()
{
int z=99;
x(&z);
}
========================================
I looked for some samples in books or web, no such luck. Still trying to
read up on passing pointers this way!
All help appreciated! Thanks.
when you pass the pointer you are passing the address/location of
where the value is so in your example above when you pass &z to x you
are passing the address of the variable z to x.
when you further want to pass the address to yet another function from
within x() you can pass the pointer, but in y() above you are passing
the value (*a)
normally you only pass the address if you want to modify the value
from within your function.
void x(int *p)
{
y(p); /* p is the address of z, *p is the value 99 */
}
void y(int *p)
{
int x = *p; /* p is still address of z and *p value of z */
}
void main()
{
int z = 99; /* value of z is 99, address of z on the stack is &z */
x( &z );
}
hth/ajk
- Follow-Ups:
- Re: Passing pointers?
- From: ajk
- Re: Passing pointers?
- References:
- Passing pointers?
- From: Robby
- Re: Passing pointers?
- From: ajk
- Passing pointers?
- Prev by Date: Re: is such exception handling approach good?
- Next by Date: Re: is such exception handling approach good?
- Previous by thread: Re: Passing pointers?
- Next by thread: Re: Passing pointers?
- Index(es):
Relevant Pages
|