帮助w / c / c ++问题 [英] help w/ c/c++ problem

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

问题描述




28b(宏)。写一个宏sizeof(var),返回其参数的大小

。你是不允许的,使用sizeof()运算符来支付
这个。为了额外的信用,写另一个宏,sizeof_t(类型),

返回一个类型的大小。


给出的是以下模板.... ......


//////////////////////////////// //////////////////////////////////////

// ASS28B。 C

///////////////////////////////////////// /////////////////////////////


#include< Stdio.H>


#define SIZEOF(var)//填写宏文本

void main()

{

char a;

short b;

int c;

long d;

long long e;

struct x *(* f)(struct y *);

union {int a [100]; float b [100]; } g;

void * h;


printf(" sizeof a:%i \ n",SIZEOF(a));

printf(" sizeof b:%i \ n",SIZEOF(b));

printf(" sizeof c:%i \ n",SIZEOF(c) ));

printf(" sizeof d:%i \ n",SIZEOF(d));

printf(" sizeof e:%i \ n",SIZEOF(e));

printf(" sizeof f:%i \ n",SIZEOF(f));

printf(" sizeof g:%i \ n",SIZEOF(g));

printf(" sizeof h:%i \ n",SIZEOF(h));

}


//////////////////////////////////// //////////////////////////////////

你能告诉我这个吗?


tnx !!!

-

mfg,heinrich :)



28b (macros). write a macro, sizeof(var), that returns the size of
its argument . you are not allowed, to use the sizeof() operator for
this . for extra credit, write another macro, sizeof_t(type), that
returns the size of a type .

given is the following template..........

//////////////////////////////////////////////////////////////////////
// ASS28B.C
//////////////////////////////////////////////////////////////////////

#include <Stdio.H>

#define SIZEOF( var ) // FILL IN MACRO TEXT

void main()
{
char a;
short b;
int c;
long d;
long long e;
struct x* (*f)(struct y*);
union { int a[100]; float b[100]; } g;
void* h;

printf("sizeof a : %i\n", SIZEOF(a));
printf("sizeof b : %i\n", SIZEOF(b));
printf("sizeof c : %i\n", SIZEOF(c));
printf("sizeof d : %i\n", SIZEOF(d));
printf("sizeof e : %i\n", SIZEOF(e));
printf("sizeof f : %i\n", SIZEOF(f));
printf("sizeof g : %i\n", SIZEOF(g));
printf("sizeof h : %i\n", SIZEOF(h));
}

//////////////////////////////////////////////////////////////////////
can u hlp me w/ this ?

tnx !!!
--
mfg, heinrich :)

推荐答案

Heinrich Pumpernickel说:
Heinrich Pumpernickel said:

28b(宏)。写一个宏sizeof(var),返回其参数的大小


28b (macros). write a macro, sizeof(var), that returns the size of
its argument .



#define SIZEOF(x)(sizeof(x))

#define SIZEOF(x) (sizeof(x))


你不被允许,到为此使用sizeof()运算符。
you are not allowed, to use the sizeof() operator for this .



为什么不呢?这就是它的* *。

Why not? That''s what it''s *for*.


给出的是以下模板..........
given is the following template..........



< snip>

<snip>


>

void main()
>
void main()



如果你的导师给了你这个模板,你需要一个新的教师。在

C中,main返回int。


< snip>


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - 1999年7月29日dmr

If your instructor gave you this template, you need a new instructor. In
C, main returns int.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


7月1日上午5:04,Richard Heathfield< r ... @ see.sig.invalidwrote:
On Jul 1, 5:04 am, Richard Heathfield <r...@see.sig.invalidwrote:

Heinrich Pumpernickel说:
Heinrich Pumpernickel said:

28b(宏)。写一个宏sizeof(var),返回其参数的大小


28b (macros). write a macro, sizeof(var), that returns the size of
its argument .



#define SIZEOF(x)(sizeof(x))


#define SIZEOF(x) (sizeof(x))



但我不允许这么做

but i m not allowed to do that


>
>

您不允许使用sizeof()运算符。
you are not allowed, to use the sizeof() operator for this .



为什么不呢?这就是它的* *。


Why not? That''s what it''s *for*.



这只是一个例外

well its just an excersise


>
>

给出的是以下模板..........
given is the following template..........



< ; snip>


<snip>


void main()
void main()



如果你的导师给你这个模板,你需要一个新教练。在

C中,main返回int。


If your instructor gave you this template, you need a new instructor. In
C, main returns int.



我不相信。我的c / c ++老师(非常有经验的程序员)

并且这本书都使用了两个说如果main没有返回

值,那么它可以被声明为void!

i dont believe that . my c/c++ teacher (very experienced programmer)
AND the book were using both say that if main does not return a
value it can be declared void !


< snip>


-

Richard Heathfield< http://www.cpax .org.uk>

电子邮件:-www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - - 1999年7月29日
<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999



-

mfg,heinrich :)

--
mfg, heinrich :)


Heinrich Pumpernickel说:
Heinrich Pumpernickel said:

7月1日上午5:04,Richard Heathfield< r ... @ see.sig.invalidwrote:
On Jul 1, 5:04 am, Richard Heathfield <r...@see.sig.invalidwrote:

> Heinrich Pumpernickel说:
>Heinrich Pumpernickel said:

28b(宏)。写一个宏sizeof(var),返回其参数的大小


28b (macros). write a macro, sizeof(var), that returns the size of
its argument .


#define SIZEOF(x)(sizeof(x))


#define SIZEOF(x) (sizeof(x))



但我不允许这样做


but i m not allowed to do that


>>
>>

您不允许使用sizeof()运算符。
you are not allowed, to use the sizeof() operator for this .


为什么不呢?这就是它* * * *


Why not? That''s what it''s *for*.



以及它只是一个特殊的

well its just an excersise



我希望演习的重点是要证明没有令人满意的便携式解决方案,除了改变规则并允许使用sizeof运算符。


您可以为对象执行此操作:


#define SIZEOF(x)((unsigned char *)(& ;(x)+1) - ((unsigned char *)&(x))


但是你不能在没有依赖

未定义的行为,如果你能避免它就永远不会明智,而且这里使用sizeof可以轻松避免它。

I hope that the point of the exercise is to demonstrate that there is no
satisfactory, portable solution to the problem, other than to change
the rules and allow use of the sizeof operator.

You can just about do it for objects:

#define SIZEOF(x) ((unsigned char *)(&(x)+1)-((unsigned char *)&(x))

but you can''t do it for expressions or types without relying on
undefined behaviour, which is never wise if you can avoid it, and here
it can be avoided with ease by using sizeof.


给出的是以下模板..........
given is the following template..........


< snip> ;


<snip>


void main()
void main()


如果你的我nstructor为你提供了这个模板,你需要一个新的教师。
在C中,main返回int。


If your instructor gave you this template, you need a new instructor.
In C, main returns int.



我不相信。


i dont believe that .



然后通过阅读相关的语言定义来了解

规范,ISO / IEC 9899.

Then find out for yourself, by reading the relevant language definition
specification, ISO/IEC 9899.


我的c / c ++老师(非常有经验的程序员)
my c/c++ teacher (very experienced programmer)



也许他是,但他对C不太了解他甚至不能正确地获得

入口点。

Perhaps he is, but he doesn''t know much about C if he can''t even get the
entry point right.


这本书正在使用两者说如果main不返回

值可以宣布无效!
AND the book were using both say that if main does not return a
value it can be declared void !



然后得到一本更好的书。更好的是,获得ISO / IEC 9899.


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Then get a better book. Better still, get ISO/IEC 9899.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


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

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