Valhalla Legends Forums Archive | General Programming | [C] Multiple Indirection

AuthorMessageTime
MrRaza
Can anyone expand this subject to me clearly, it's a little confusing. I think for the most part that it is basically where you have a pointer point to another pointer that points to the target value? am i right? Should I show a code snipplet so you understand what I'm saying?
April 16, 2003, 12:34 PM
iago
You're exactly right, it's just a pointer to a pointer.

[code]int a = 6;
int *b = a;
int **c = b;

printf("%d%d%d\n", a, *b, **c);
**c++;
*b *= 4;
printf("%d%d%d\n", a, *b, **c);
[/code]

If I'm right, that should output
666
282828

(thought I probably made some horrible mistake)
April 16, 2003, 4:37 PM
Skywing
[quote author=iago link=board=5;threadid=1074;start=0#msg7956 date=1050511054]
You're exactly right, it's just a pointer to a pointer.

[code]int a = 6;
int *b = a;
int **c = b;

printf("%d%d%d\n", a, *b, **c);
**c++;
*b *= 4;
printf("%d%d%d\n", a, *b, **c);
[/code]

If I'm right, that should output
666
282828

(thought I probably made some horrible mistake)
[/quote]

I think you'll need to use the address-of operator to initialize b and c like that.
April 16, 2003, 4:47 PM
iago
Yup, skywing's right, it should be this:
[code]int a = 6;
int *b = &a;
int **c = &b;

printf("%d%d%d\n", a, *b, **c);
**c++;
*b *= 4;
printf("%d%d%d\n", a, *b, **c);
[/code]
April 16, 2003, 8:54 PM
Yoni
If you turn that **c++ into (**c)++, sure
April 17, 2003, 3:31 PM
MrRaza
I also need some help with Pointers-to-functions(Function pointers), I don't really know where to start. Could someone give em some example code and explain it? If not, dont flame/reply.
April 17, 2003, 4:07 PM
Yoni
The simplest pseudo-syntax (?) for a pointer to a function in C is:
[code]return-type (call-convention * pointer-name)(parameter-list);[/code]
where call-convention is a keyword such as __cdecl, __stdcall, or __fastcall (in VC++).

Example:
[code]int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }

int math(int a, int b, char op)
{
int (* func)(int a, int b) = NULL;
/* This declares a variable called 'func', and initializes it to NULL
** It is a pointer to a function that has 2 int parameters,
** and returns int
*/

switch(op) {
case '+':
func = &add; // The & is optional, but recommended...
break;
case '-':
func = subtract; // So the compiler should accept this, but might raise a warning
break;
default:
return 0; // or whatever
}

return func(a, b); // Call it like you call a function
// return (*func)(a, b); // This style is also allowed
}[/code]

You can use a type-definition to simplify the above code:
[code]typedef int (* MathBinaryOperation)(int a, int b);
/* The type 'MathBinaryOperation' is a pointer to a function
** that has 2 int parameters and returns int
*/
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }

int math(int a, int b, char op)
{
MathBinaryOperation func = NULL; // Same as before, but better looking syntax

switch(op) {
case '+':
func = &add;
break;
case '-':
func = subtract;
break;
default:
return 0;
}

return func(a, b);
}[/code]
April 17, 2003, 4:29 PM

Search