共享指针??? [英] Shared pointers???

查看:50
本文介绍了共享指针???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



C中的
......有没有任何这样的东西。键入指针?在下面的示例

中,有什么方法可以定义函数x接受一个指针

类型为struct a的或者struct b,或者我可能用

和int i创建的任何其他结构,所以我不必创建多个完全正确的函数

同样的事情?让我们假设我有一个有效的理由,可以将具有类似数据的单独的

结构。我只想分享功能x。并且

消除函数y。我可以有更多类似的结构,我会想要使用泛型函数x。对于他们所有人。有什么方法可以吗?

那个?

#include< stdio.h>

//结构" a"

typedef struct

{

int i;

} a;

//结构" b"

typedef struct

{

int i;

char s [5 ];

} b;

//功能" x"

void x(a * p)

{

(* p).i = 10;

}

//功能y

void y(b * p)

{

(* p).i = 20;

}

int main(无效)

{

a a1;

b b1;

x(& a1);

y(& b1);

printf(" a1.i =%d,b1.i =%d \ n",a1.i,b1.i );

返回0;

}

Hi,

in C... is there any such thing as an "any" type pointer? In the example
below, is there any way that I could define function "x" to accept a pointer
of type "struct a" or "struct b", or any other structure I might create with
an "int i", so I wouldn''t have to create multiple functions that do exactly
the same thing? Let''s assume I have a valid reason for having separate
structures with similar data. I would just like to share function "x" and
eliminate function "y". I could have many more similar structures and I''d
like to use generic function "x" for all of them. Is there any way I can do
that?

#include <stdio.h>
// Structure "a"
typedef struct
{
int i;
} a;
// Structure "b"
typedef struct
{
int i;
char s[5];
} b;
// Function "x"
void x(a *p)
{
(*p).i = 10;
}
// Function "y"
void y(b *p)
{
(*p).i = 20;
}
int main(void)
{
a a1;
b b1;
x(&a1);
y(&b1);
printf("a1.i = %d, b1.i = %d\n", a1.i, b1.i);
return 0;
}

推荐答案

gedumer1写道:
gedumer1 wrote:



$ C $ b在C ...中是否有任何诸如any之类的东西键入指针?
Hi,

in C... is there any such thing as an "any" type pointer?



有'void *'',但它不会做你想要的。

There''s `void*'', but it won''t do what you want.


在下面的示例

中,有什么方法可以定义函数x接受一个指针

类型为struct a的或struct b,
In the example
below, is there any way that I could define function "x" to accept a pointer
of type "struct a" or "struct b",



函数参数只有一种类型。您可以使用其他类型的参数表达式来调用它,但是在函数看到它们之前它们将转换为参数的类型。

也就是说,该函数可以接受任何可转换的参数

到相应参数的类型,但实际上接收的函数是参数类型的值。


函数无法在转换之前发现参数类型

表达式。

A function parameter has one and only one type. You may be able
to call it with argument expressions of other types, but they will
be converted to the parameter''s type before the function sees them.
That is, the function can "accept" any argument that is convertible
to the type of the corresponding parameter, but what the function
actually receives is a value of the parameter''s type.

There is no way the function can discover the type of the argument
expression prior to conversion.


或者我可能使用

和int i创建的任何其他结构,所以我不必创建多个函数,这些函数完全相同吗?b $ b b同样的事情?让我们假设我有一个有效的理由,可以将具有类似数据的单独的

结构。我只想分享功能x。并且

消除函数y。我可以有更多类似的结构,我会想要使用泛型函数x。对于他们所有人。有什么方法可以吗

那个?

[...代码剪断...]
or any other structure I might create with
an "int i", so I wouldn''t have to create multiple functions that do exactly
the same thing? Let''s assume I have a valid reason for having separate
structures with similar data. I would just like to share function "x" and
eliminate function "y". I could have many more similar structures and I''d
like to use generic function "x" for all of them. Is there any way I can do
that?
[... code snipped ...]



如果结构类型都具有公共初始序列,则

元素(例如,如果它们都以'int''开头跟随

由'char *''跟随可能不同的东西),那么你

可以将所有结构类型放入一个联合中,并将函数交给一个

指向联合的指针。然后,该函数可以使用常见的

元素,而不管目前实际上是什么类型的结合,




避免联合的方法是将公共元素

放在它们自己的小结构中,并使该结构成为每个真实结构的第一个元素




struct common {int i; char * cp; };

struct type1 {struct common c;双d; };

struct type2 {struct common c; float fa [10];然后你可以将函数指向每个

结构的`c''元素,并且函数可以访问元素`c''没有

担心什么样的超级结构包含它。


-
Er ********* @ sun.com


gedumer1写道:
gedumer1 wrote:



C中的
......有没有any这样的东西。键入指针?
Hi,

in C... is there any such thing as an "any" type pointer?



是的,只需使用void * p。但是,你必须跟踪它指向的对象是什么类型的对象,你必须在取消引用它之前将它改回原来的

指针类型:


void set_to_one(void * p,enum pointer_type type)

{

开关(类型)

{

案例INT_TYPE:

{

*(int *)p = 1;

}

案例DOUBLE_TYPE:

{

*(double *)p = 1.0;

}

//等等

}

}


在示例中

Yes, just use void*p. However, you have to keep track of what type of
object it points at, and you''ll have to change it back to the original
pointer type before dereferencing it:

void set_to_one(void *p, enum pointer_type type)
{
switch(type)
{
case INT_TYPE:
{
*(int*)p = 1;
}
case DOUBLE_TYPE:
{
*(double*)p = 1.0;
}
// etc.
}
}

In the example


下面,有什么办法可以定义函数x接受一个指针

类型为struct a的或者struct b,或者我可能用

和int i创建的任何其他结构,所以我不必创建多个完全正确的函数

同样的事情?让我们假设我有一个有效的理由,可以将具有类似数据的单独的

结构。我只想分享功能x。并且

消除函数y。我可以有更多类似的结构,我会想要使用泛型函数x。对于他们所有人。有什么方法可以吗?

那个?

#include< stdio.h>

//结构" a"

typedef struct

{

int i;

} a;

//结构" b"

typedef struct

{

int i;

char s [5 ];

} b;

//功能" x"

void x(a * p)

{

(* p).i = 10;

}

//功能y

void y(b * p)

{

(* p).i = 20;

}

int main(无效)

{

a a1;

b b1;

x(& a1);

y(& b1);

printf(" a1.i =%d,b1.i =%d \ n",a1.i,b1.i );

返回0;

}
below, is there any way that I could define function "x" to accept a pointer
of type "struct a" or "struct b", or any other structure I might create with
an "int i", so I wouldn''t have to create multiple functions that do exactly
the same thing? Let''s assume I have a valid reason for having separate
structures with similar data. I would just like to share function "x" and
eliminate function "y". I could have many more similar structures and I''d
like to use generic function "x" for all of them. Is there any way I can do
that?

#include <stdio.h>
// Structure "a"
typedef struct
{
int i;
} a;
// Structure "b"
typedef struct
{
int i;
char s[5];
} b;
// Function "x"
void x(a *p)
{
(*p).i = 10;
}
// Function "y"
void y(b *p)
{
(*p).i = 20;
}
int main(void)
{
a a1;
b b1;
x(&a1);
y(&b1);
printf("a1.i = %d, b1.i = %d\n", a1.i, b1.i);
return 0;
}



出于上述代码的目的,你不要''我需要像我一样使用复杂的

开关语句。你的函数x()只使用结构的第一个

成员,在所有情况下都是一样的。有几种使用这个事实的方法:


//选项1:这适用于任何结构的任何成员

void x(int * p){* p = 10;}

x(& a1-> i);


//选项2:这只适用于结构的第一个成员

void x(void * p){*(int *)p = 10;}

x(& a1) ;


//选项3:这适用于普通初始

序列中的任何元素

//不同结构类型之间共享的成员。

typedef union

{

a a1;

b b1;

} u;


void x(u * p){p-> ai = 10;}

void x(u * p){p-> ai = 20;}

//由于公共初始序列规则,这些函数是否使用

// p-> a.i或p-> b.i是无关紧要的。任何一个都同样可以接受。


u u1;

u u2;

x(& u1);
$ b在C中的$ by(& u2);

For the purposes of your code above, you don''t need the complicated
switch statement like I used. Your function x() only uses the first
member of the structure, which is the same in all cases. There are
several ways to use this fact:

// Option 1: this will work for any member of any struct
void x(int *p) { *p = 10;}
x(&a1->i);

// Option 2: this will work only for the first member of the struct
void x(void*p) { *(int *)p = 10;}
x(&a1);

// Option 3: This will work for any element in the "common initial
sequence"
// of members shared between the different struct types.
typedef union
{
a a1;
b b1;
} u;

void x(u*p) { p->a.i = 10;}
void x(u*p) { p->a.i = 20;}
// Because of the "common initial sequence" rule, it doesn''t matter
whether those functions use
// p->a.i or p->b.i; either one is equally acceptable.

u u1;
u u2;
x(&u1);
y(&u2);


> ...是否有任何诸如any之类的东西。键入指针?
>in C... is there any such thing as an "any" type pointer?

>

有'void *'',但它不会做你想要的。
>
There''s `void*'', but it won''t do what you want.



谢谢......这是关键。看下面的代码。除非我没想到,否则我认为它解决了这个问题。

Thanks... that''s the key. Look at the code below. Unless I''m not thinking
straight, I think it solves the problem.


struct common {int i; char * cp; };

struct type1 {struct common c;双d; };

struct type2 {struct common c; float fa [10];然后你可以将函数指向每个

结构的`c''元素,并且函数可以访问元素`c''没有

担心什么样的超级结构包含它。
struct common { int i; char *cp; };
struct type1 { struct common c; double d; };
struct type2 { struct common c; float fa[10]; };

You can then pass the function a pointer to the `c'' element of each
struct, and the function can access the elements of the `c'' without
worrying about what kind of super-struct contains it.



使用通用结构,将上面描述的内容排除在外,这将包含我想要包含在内的所有数据类型。标准功能应该做什么

诀窍......你同意吗?


/ **************** /

#include< stdio.h>

typedef struct

{

int i;

}通用;

typedef struct

{

int i;

} a;

typedef struct

{

int i;

char s [5];

} b;

void y(void * p,int i)

{

generic * any;

any = p;

any-> i = i;

}

int main(无效)

{

a a1;

b b1;


y(& a1,10);

y (& b1,20);

printf(" a1.i =%d,b1.i =%d \ n",a1.i,b1.i);
返回0;

}

/ **************** /

结果是:


a1.i = 10,b1.i = 20

Using a generic structure, sort of what you describe above, that will house
all of the data types I want to include in the standard function should do
the trick... do you agree?

/****************/
#include <stdio.h>
typedef struct
{
int i;
} generic;
typedef struct
{
int i;
} a;
typedef struct
{
int i;
char s[5];
} b;
void y(void *p, int i)
{
generic *any;
any = p;
any->i = i;
}
int main(void)
{
a a1;
b b1;

y(&a1, 10);
y(&b1, 20);
printf("a1.i = %d, b1.i = %d\n", a1.i, b1.i);
return 0;
}
/****************/

The result is:

a1.i = 10, b1.i = 20


这篇关于共享指针???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆