Re: Passing pointers?



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

.



Relevant Pages

  • Re: confusion: casting function pointers
    ... pointer from the 'actual/other modules' that takes arguments of type ... list to types of void *). ... int main{ ... without a prototype, a number of special "promotion" rules take ...
    (comp.lang.c)
  • Re: invalid pointer adress
    ... >> pointer causes the program to exit with a core dump. ... struct s2{void *p;}; ... int leseExterneHinweise_masch_storno ... typedef struct s_AusdatFeldbeschreibung ...
    (comp.lang.c)
  • Should io(read|write)(8|16|32)_rep take (const|) volatile u(8|16|32) __iomem *addr?
    ... the destination pointer on user-space ... @src: ... const void *data, int bytelen); ...
    (Linux-Kernel)
  • Re: function pointer help!
    ... //the return void and input prameters are defined in the manual... ... void MyProjectView::CallHandler(int,unsigned int, unsigned int, void*) ... You are attempting to use a C++ member function as the callback, but the callback is defined in terms or C, not C++. ... The underlying problem is that C++ functions receive a hidden parameter, the 'this' pointer, so their signature is incompatible with C definitions. ...
    (microsoft.public.vc.mfc)
  • Re: Whats the meaning of this code
    ... void * work ... Whats the meaning of this line:- ... The meaning...a function called work that accepts a pointer of type ... pointer value is cast to an int value. ...
    (comp.lang.c)