11-16-2006 07:28 AM
11-16-2006 07:37 AM
11-16-2006 07:52 AM
11-16-2006 08:03 AM - edited 11-16-2006 08:03 AM
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
11-16-2006 08:04 AM
11-16-2006 08:21 AM
You would then need:
*b = Data[a];
JR
11-16-2006 08:23 AM
11-16-2006 08:28 AM - edited 11-16-2006 08:28 AM
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
11-16-2006 08:46 AM
11-16-2006 09:14 AM - edited 11-16-2006 09:14 AM
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