LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Why can't I use a pointer in an argument?

Here is an example of a function...

// Function prototype
int foobar(unsigned int a, int *b);

// Function
int foobar(unsigned int a, int &b)
{
    // Acquire some data... then
    b = Data[a];
}

I get the following error:
73, syntax error; found '&' expecting ')'.

This is the normal method of passing in a pointer to an integer to a function and then setting the contents of the integer in the function. Why doesn't CVI let me do this? Is there another way?
0 Kudos
Message 1 of 15
(4,370 Views)
Well, in function definition you correctly used the indirect operator (*) while in the function itself you used the address operator instead (&): could it be a transcription error?


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 15
(4,362 Views)
Hi!
  Roberto, as usual, is rightSmiley Wink.  More, the function prototype line and the function definition should have the same syntax (same name of variables, and same type!)....try to just change & into * ,  (take care on how Data is defined.....).

Graziano
0 Kudos
Message 3 of 15
(4,358 Views)

Hi Mikie,

I believe that we all have done errors when using pointers for the first time.

Try this code:

// Function prototype
int foobar(unsigned int a, int *b);

// Function
int foobar(unsigned int a, int *b)
{
    // Acquire some data... then
    *b = Data[a];  // If you want to copy the content of the element a of the tab into b.

      b = &Data[a]; .// If you want that b points to the element a of the table
}

Here is a link to the use of pointers   ______ Bad point for me, it is in French (gonna find an other one)

Regards,

Message Edité par Mathieu Steiner le 11-16-2006 08:10 AM

Message 4 of 15
(4,356 Views)
Well, the function I outlined has been working for me for 20 years.

If I change to the following:

// Function prototype
int foobar(unsigned int a, int *b);

// Function
int foobar(unsigned int a, int *b)
{
    // Acquire some data... then
    b = Data[a];
}


Then then following error occurs on the line trying to set b: Operands of = have illegal types 'pointer to in' and 'int'.
0 Kudos
Message 5 of 15
(4,358 Views)

You would then need:

    *b = Data[a];

JR

Message 6 of 15
(4,338 Views)
It seems that I can't edit my post, so I'll continue.

It's obvious that in my last example, I should get an error. Both sides are of different type.

Now, I'll admit that 20 years ago I was confused by pointers when I first used them, and I've stayed away from them ever since when possible. But that certainly didn't help me learn any. My first example does work, and it is a correct use. It just doesn't work under CVI.

Now if I use *b = Data[a];

If b already a pointer to b, then isn't *b a pointer to a pointer to b? Or it it different on the left side of an assignment. Is it a de-reference?

Mike
0 Kudos
Message 7 of 15
(4,337 Views)

You are confusing & and *. & gives you a pointer to, or the address of, a variable. * gives you the contents of whatever is being pointed to, otherwise known as a de-reference.

JR

edit: Of course when you actually call your function, you would then use the & operator, as in: foobar (x, &y); - maybe this was causing some confusion?

Message Edited by jr_2005 on 11-16-2006 02:32 PM

Message 8 of 15
(4,334 Views)
I´m also not so familiar with those pointer, but just to sum it up and to see if I´m right:

// Function prototype
int foobar(unsigned int a, int *b);

// Function
int foobar(unsigned int a, int *b)
{
    // Acquire some data... then
    *b = Data[a];
}

//Function call version 1
int a, b;
foobar (a,&b);

//Function call version 2
int a, *b;
foobar (a,b);

is that correct this way?
0 Kudos
Message 9 of 15
(4,326 Views)

Yes, but in your version 2 code you need to initialise the b pointer - just saying it is a pointer is OK for the compiler, but not enough for the program to work properly. You need to get it to point to something eg:

    //Function call version 2
    int a, *b, z;
    b = &z;

    foobar (a, b);

Then the function would effectively put its output (Data[a]) into the variable z.

JR

Message Edited by jr_2005 on 11-16-2006 03:14 PM

0 Kudos
Message 10 of 15
(4,320 Views)