Passing of pointers of different types to a function using singleparameter at same time.


M

manu

Hi all,

I have a query regarding the passing of pointers of different types to
a function using single parameter at same time.

Suppose i have

struct type1 abc;
struct type2 xyz;

My intention is to pass the address of the structures to a function.

I tried

- Before the invocation a void pointer array is initialised.

void *ptr[2] = {
(void *)&abc,
(void *)&xyz
}

Invoked the function
fun1(ptr);

BTW the function signature is fun1(void *vptr)

In this case wont be able to re-direct the void pointer to get the
individual pointers. I tried with character pointers but they are
giving warnings.

Please explain if any method exists. Hoping for replies.
 
Ad

Advertisements

M

Mark Bluemel

[I've slightly reordered his post, for what I hope is more clarity]
Hi all,

I have a query regarding the passing of pointers of different types to
a function using single parameter at same time.

It's often more helpful to explain what you are trying to achieve
rather than just telling us how you are trying to achieve it. I can't
easily see what problem you are trying to address.
Suppose i have

struct type1 abc;
struct type2 xyz;
OK.

My intention is to pass the address of the structures to a function.

I think you mean the addresses not the address....
BTW the function signature is fun1(void *vptr)

Why? If you want to pass multiple addresses (the addresses of two
structures), why would the function be coded for one address?
I tried

- Before the invocation a void pointer array is initialised.

void *ptr[2] = {
(void *)&abc,
(void *)&xyz

}

so the type of ptr is "void **" (or "void * []") - right? This is not
the type that fun1 is coded to receive.
Invoked the function
fun1(ptr);

In this case wont be able to re-direct the void pointer to get the
individual pointers. I tried with character pointers but they are
giving warnings.

Please explain if any method exists. Hoping for replies.

Code fun1 to take (an) appropriate argument(s). e.g. "void fun1(void
**ptrs)"
 
B

Beej Jorgensen

manu said:
struct type1 abc;
struct type2 xyz;

When the pointers come in as void*s, you need some kind of way to figure
out what to cast them to.

One option is to do something like this:

#define STRUCT_TYPE1 1
#define STRUCT_TYPE2 2

void fun2(void *p, int type)
{
struct type1 *pt1;
struct type2 *pt2;

switch (type) {
case STRUCT_TYPE1:
pt1 = p;
...
break;
case STRUCT_TYPE2:
pt2 = p;
...
break;
}
}

...
fun2(&abc, STRUCT_TYPE1);
fun2(&xyz, STRUCT_TYPE2);

Another option is to embed the type information in the structs:

struct type0 {
int type;
};

struct type1 {
int type;
int payload;
};

struct type2 {
int type;
float fpayload;
};

void fun3(struct type0 *p)
{
struct type1 *pt1;
struct type2 *pt2;

switch (p->type) {
case STRUCT_TYPE1:
pt1 = (struct type1*)p;
...
break;
case STRUCT_TYPE2:
pt2 = (struct type2*)p;
...
break;
}
}

...
abc.type = STRUCT_TYPE1;
abc.payload = 3490;
fun3((struct type0 *)&abc);

xyz.type = STRUCT_TYPE2;
xyz.fpayload = 3.14159;
fun3((struct type0 *)&xyz);

Hopefully I haven't screwed any of that up over my morning coffee.

Another poster noted that you were actually passing the wrong type into
the function, as well. I don't know what your intent was.

-Beej
 
P

Paul N

Hi all,

I have a query regarding the passing of pointers of different types to
a function using single parameter at same time.

Suppose i have

struct type1 abc;
struct type2 xyz;

My intention is to pass the address of the structures to a function.

I tried

- Before the invocation a void pointer array is initialised.

void *ptr[2] = {
(void *)&abc,
(void *)&xyz

}

Invoked the function
fun1(ptr);

BTW the function signature is fun1(void *vptr)

In this case wont be able to re-direct the void pointer to get the
individual pointers. I tried with character pointers but they are
giving warnings.

Please explain if any method exists. Hoping for replies.

Since you are trying to pass an array of pointers, it's probably
easier to pass an array of pointers to void. The following code does
that for fun1. But, as a pointer to most things can be converted to a
pointer to void and back, it is possible to use a pointer to void if
you really must do it that way. fun2 and fun3 do this - one uses an
extra variable in the converting back, to make things a little less
messy, while the other goes the whole hog.


#include <stdio.h>

struct type1 { int a; int b; };
struct type2 { char x; char y; };

struct type1 abc;
struct type2 xyz;

void fun1(void *vptr[]) {
printf("b is %d, y is %c\n",
( (struct type1 *) vptr[0] ) -> b,
( (struct type2 *) vptr[1] ) -> y);
}

void fun2(void *vptr) {
void **vpp = vptr;
printf("b is %d, y is %c\n",
( (struct type1 *) vpp[0] ) -> b,
( (struct type2 *) vpp[1] ) -> y);
}

void fun3(void *vptr) {
printf("b is %d, y is %c\n",
( (struct type1 *) (((void **) vptr)[0]) ) -> b,
( (struct type2 *) (((void **) vptr)[1]) ) -> y);
}


int main(void) {

void *ptr[2] = {
(void *)&abc,
(void *)&xyz };

abc.a = 10;
abc.b = 20;
xyz.x = 'X';
xyz.y = 'Y';


fun1(ptr);
fun2(ptr);
fun3(ptr);

return 0;
}


But, as Beej pointed out, if you don't know what sorts of struct you
will be passing, then you need some way to tell the functions.
Whereas, if you do know, you could just pass them by the type that
they are. So do have a think about what you are trying to do.

Hope this helps.
Paul.
 
M

manu

I have a query regarding the passing of pointers of different types to
a function using single parameter at same time.
Suppose i have
struct type1 abc;
struct type2 xyz;
My intention is to pass the address of the structures to a function.
- Before the invocation a void pointer array is initialised.
void *ptr[2] = {
(void *)&abc,
(void *)&xyz

Invoked the function
fun1(ptr);
BTW the function signature is fun1(void *vptr)
In this case wont be able to re-direct the void pointer to get the
individual pointers. I tried with character pointers but they are
giving warnings.
Please explain if any method exists. Hoping for replies.

Since you are trying to pass an array of pointers, it's probably
easier to pass an array of pointers to void. The following code does
that for fun1. But, as a pointer to most things can be converted to a
pointer to void and back, it is possible to use a pointer to void if
you really must do it that way. fun2 and fun3 do this - one uses an
extra variable in the converting back, to make things a little less
messy, while the other goes the whole hog.

#include <stdio.h>

struct type1 { int a; int b; };
struct type2 { char x; char y; };

struct type1 abc;
struct type2 xyz;

void fun1(void *vptr[]) {
printf("b is %d, y is %c\n",
  ( (struct type1 *) vptr[0] ) -> b,
  ( (struct type2 *) vptr[1] ) -> y);

}

void fun2(void *vptr) {
void **vpp = vptr;
printf("b is %d, y is %c\n",
  ( (struct type1 *) vpp[0] ) -> b,
  ( (struct type2 *) vpp[1] ) -> y);

}

void fun3(void *vptr) {
printf("b is %d, y is %c\n",
  ( (struct type1 *) (((void **) vptr)[0]) ) -> b,
  ( (struct type2 *) (((void **) vptr)[1]) ) -> y);

}

int main(void) {

void *ptr[2] = {
(void *)&abc,
(void *)&xyz };

abc.a = 10;
abc.b = 20;
xyz.x = 'X';
xyz.y = 'Y';

fun1(ptr);
fun2(ptr);
fun3(ptr);

return 0;

}

But, as Beej pointed out, if you don't know what sorts of struct you
will be passing, then you need some way to tell the functions.
Whereas, if you do know, you could just pass them by the type that
they are. So do have a think about what you are trying to do.

Hope this helps.
Paul.- Hide quoted text -

- Show quoted text -

Hi,
The mistake i did was on type of the argument of the function. Thanks
Paul,Beej and Mark for the clarification.
 
Ad

Advertisements

N

Nick Keighley

Hi all,

I have a query regarding the passing of pointers of different types to
a function using single parameter at same time.

Suppose i have

struct type1 abc;
struct type2 xyz;

My intention is to pass the address of the structures to a function.

I tried

- Before the invocation a void pointer array is initialised.

void *ptr[2] = {
(void *)&abc,
(void *)&xyz

}

Invoked the function
fun1(ptr);

BTW the function signature is fun1(void *vptr)

In this case wont be able to re-direct the void pointer to get the
individual pointers. I tried with character pointers but they are
giving warnings.

Please explain if any method exists. Hoping for replies.

Could you use a struct? It's best to avoid void* if you can

struct type1 abc;
struct type2 xyz;

struct args
{
struct type1 *t1;
struct type2 *t2;
};

struct args my_args[] = {&abc, &xyz};

void fun (struct args formal_args[]);

int main (void)
{
fun (args);
return 0;
}
 
Ad

Advertisements


Top