• Welcome to Valhalla Legends Archive.
 

[C] Multiple Indirection

Started by MrRaza, April 16, 2003, 07:34 AM

Previous topic - Next topic

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?

iago

You're exactly right, it's just a pointer to a pointer.  

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);


If I'm right, that should output
666
282828

(thought I probably made some horrible mistake)
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Skywing

Quote from: iago on April 16, 2003, 11:37 AM
You're exactly right, it's just a pointer to a pointer.  

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);


If I'm right, that should output
666
282828

(thought I probably made some horrible mistake)

I think you'll need to use the address-of operator to initialize b and c like that.

iago

Yup, skywing's right, it should be this:
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);
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Yoni

If you turn that **c++ into (**c)++, sure

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.

Yoni

The simplest pseudo-syntax (?) for a pointer to a function in C is:
return-type (call-convention * pointer-name)(parameter-list);
where call-convention is a keyword such as __cdecl, __stdcall, or __fastcall (in VC++).

Example:
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
}


You can use a type-definition to simplify the above 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);
}