C初学者问题 [英] C Beginner Question

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

问题描述

如何将不同类型的参数(如short,int,long,

等)传递给执行常规操作的函数?


例如:


int main()

{

短x = 1;

int y = 1;

长z = 1

bit_count(x);

bit_count(y);

bit_coutn(z);


返回0;

}


/ *如何申报函数接受不同类型的参数* /


int bit_count()

{

int count = 0;


while(x!= 0)

{

printf(" count =%d和x =%d \的值) n",count ++,x);

x<< = 1;

}

返回0;

}


谢谢

How do I pass different types of argument such as short, int, long,
etc.. to a function that does routine stuff?

Example:

int main()
{
short x=1;
int y =1;
long z=1

bit_count(x);
bit_count(y);
bit_coutn(z);

return 0;
}

/* how to declare a function to accept different types of argument */

int bit_count()
{
int count=0;

while (x!=0)
{
printf("count = %d and value of x = %d\n", count++, x);
x<<=1;
}
return 0;
}

thanks

推荐答案

ca ***** @ sbcglobal.net (Cao Tran)在

新闻中写道:d2 ************** ************@posting.google.c om:
ca*****@sbcglobal.net (Cao Tran) wrote in
news:d2**************************@posting.google.c om:
如何将不同类型的参数(如short,int,long,
等)传递给执行常规操作的函数?


传入一个类型参数或在这个简单的情况下定义bit_count()以获取

长作为其参数。那么C将推广short和int类型,以便在对bit_count()的调用中使用
。如果你有像

结构或联合这样的复杂类型,那就不行了。

示例:

int main()
int main(void){
短x = 1;
int y = 1;
long z = 1

bit_count(x);
bit_count (y);
bit_coutn(z);

返回0;
}

/ *如何声明一个函数来接受不同类型的参数* /
How do I pass different types of argument such as short, int, long,
etc.. to a function that does routine stuff?
Pass in a type argument or in this simple case define bit_count() to take a
long as its argument. Then C will promote short and int types to long in
the call to bit_count(). This won''t work if you have complex types like
structs or unions.
Example:

int main() int main(void) {
short x=1;
int y =1;
long z=1

bit_count(x);
bit_count(y);
bit_coutn(z);

return 0;
}

/* how to declare a function to accept different types of argument */




见上文。


-

- 马克 - >

-



See above.

--
- Mark ->
--


Cao Tran写道:
Cao Tran wrote:
如何传递不同类型的参数,如short ,int,long,
等。用于执行常规操作的函数?

示例:

int main()
{
short x = 1;
int y = 1;
long z = 1

bit_count(x);
bit_count(y);
bit_coutn (z);

返回0;
}
How do I pass different types of argument such as short, int, long,
etc.. to a function that does routine stuff?

Example:

int main()
{
short x=1;
int y =1;
long z=1

bit_count(x);
bit_count(y);
bit_coutn(z);

return 0;
}




完全没有办法实现这一点。通用函数的解决方案

要求使用void *,union或可变参数函数。在任何这些情况下,你必须告诉接收函数你传递的是什么样的
变量。


一个例子是printf()。第一个参数标记了以下参数的预期类型



Brian



There''s no way to achieve this exactly. Solutions to generic functions
require either use of void*, union, or a variadic function. In any of
those cases, you''ll have to tell the receiving function what kind of
variable you are passing.

An example is printf(). The first argument flags the expected types of
the following arguments.

Brian


Cao Tran写道:
Cao Tran wrote:
如何将不同类型的参数(如short,int,long,
等)传递给执行常规操作的函数?
How do I pass different types of argument such as short, int, long,
etc.. to a function that does routine stuff?




在这种情况下,你可以通过使参数

a long int来利用促销,这样就可以接受任何整数类型。同样,你
可以使用双精度作为接受任何浮点类型的参数。


一般来说,编写一段代码的能力和然后使用

它适用于许多不同类型的功能称为* generics *,C表示

不支持。 C ++以模板的形式支持这些,并且即将发布的Java和C#的
版本支持它们。但是,通常可以执行以下任一操作:


1.通过传递void指针来编写适用于多种类型的C代码

对象以及类型标识符:


void DoGenericThing(void * object,enum typeID){

switch(typeID ){

案例TypeShape:{Shape * shape = object; / * ... * /}

case TypeCar:{Car * car = object; / * ... * /}

case TypePresident:{President * pres = object; / * ... * /}

}

}


完成此操作后,您可以增强类型C99,gcc和

安全性通过创建内联包装来支持内联函数的其他环境:

inline void DoGenericThingShape(Shape * shape){

DoGenericThing(形状,TypeShape);

}




2.如果您只是存储类型,没有对它们执行操作,

就像在某种容器中一样,那么它就足以将void指针

传递给具有该大小的对象,然后使用memmove移动周围的数据。


3.使用带有令牌粘贴的宏为您希望使用它的每种类型生成不同版本的

函数:


#define DECLARE_DOGENERICTHING(类型)\

void do_generic_thing _ ## type(type t);


#define DEFINE_DOGENERICTHING(type)\

void do_generic_thing _ ## type(type t){\

/ *实现* / \

}


然后在你想要访问的任何模块中使用两个宏

特定的实例化。将DECLARE宏放在标题中以导出

实例化,因此可以重复使用。

-

Derrick Coetzee



In this case you can take advantage of promotions by making the argument
a long int, so that any integral type will be accepted. Similarly, you
can use doubles for arguments that should accept any floating-point type.

In general though, the ability to write one piece of code and then use
it for many different types is a feature called *generics* that C does
not support. C++ supports these in the form of templates, and upcoming
versions of Java and C# support them. It is often possible, however, to
do one of the following:

1. Write C code that works on several types by passing a void pointer to
the object along with a type identifier:

void DoGenericThing(void* object, enum typeID) {
switch(typeID) {
case TypeShape: { Shape* shape = object; /* ... */ }
case TypeCar: {Car* car = object; /* ... */ }
case TypePresident: {President* pres = object; /* ... */ }
}
}

Once you''ve done this, you can enhance type safety in C99, gcc, and
other environments supporting inline functions by creating inline wrappers:
inline void DoGenericThingShape(Shape* shape) {
DoGenericThing(shape, TypeShape);
}
etc.

2. If you''re simply storing the types, not executing operations on them,
as in a container of some sort, then it suffices to pass a void pointer
to the object with the size and then use memmove to move the data around.

3. Use macros with token-pasting to generate a different version of the
function for each type you wish to use it with:

#define DECLARE_DOGENERICTHING(type) \
void do_generic_thing_##type(type t);

#define DEFINE_DOGENERICTHING(type) \
void do_generic_thing_##type(type t) { \
/* Implementation */ \
}

Then use both macros in any module you want to have access to a
particular instantiation. Put the DECLARE macro in a header to export an
instantiation, so it can be reused.
--
Derrick Coetzee


这篇关于C初学者问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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