473,544 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help w/ c/c++ problem



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

Jul 1 '07
83 2587

"Heinrich Pumpernickel" <la****@linuxma il.orgha scritto nel messaggio news:11******** **************@ n2g2000hse.goog legroups.com...
On Jul 1, 5:04 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
>Heinrich Pumpernickel said:
void main()

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

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 !
Both your C/C++ teacher (is he actually teaching you both two
languages?) and the book you're using are wrong.
Jul 1 '07 #11
Army1987 said:
>
"Richard Heathfield" <rj*@see.sig.in validha scritto nel messaggio
news:-8************** *************** *@bt.com...
>Heinrich Pumpernickel said:
>>On Jul 1, 5:04 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
Heinrich Pumpernickel said:

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

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

but i m not allowed to do that
you are not allowed, to use the sizeof() operator for this .

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

well its just an excersise

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

I've tried
#define SIZEOF(x) ((unsigned char*)(&(x)+1)-((unsigned char*)&(x))
#include <stdio.h>
int main(void)
{
register int x = 23;
printf("%d %d\n", (int)sizeof(x), (int)SIZEOF(x)) ;
return 0;
}
on my DS9K, and something very strange happened...
Yes, because you can't take the address of a register. I deduce (from
the fact that you posted at all) that you survived the experience - or
did your machine post the article on your behalf, posthumously?

Anyway, your point is valid - register objects are Yet Another Reason
Not To Do This Very Silly Thing.

--
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
Jul 1 '07 #12

"Richard Heathfield" <rj*@see.sig.in validha scritto nel messaggio news:P-*************** *************** @bt.com...
Army1987 said:
>I've tried
#define SIZEOF(x) ((unsigned char*)(&(x)+1)-((unsigned char*)&(x))
#include <stdio.h>
int main(void)
{
register int x = 23;
printf("%d %d\n", (int)sizeof(x), (int)SIZEOF(x)) ;
return 0;
}
on my DS9K, and something very strange happened...

Yes, because you can't take the address of a register. I deduce (from
the fact that you posted at all) that you survived the experience - or
did your machine post the article on your behalf, posthumously?

Anyway, your point is valid - register objects are Yet Another Reason
Not To Do This Very Silly Thing.
To be honest, the DS9K was actually MSVC, and the "something very
strange" was that it didn't compile. (The requirement for the
operand of & not to be register is a constraint.)

But I'm thinking of replacing it with:
#include <stdio.h>
#define SIZEOF(x) ((unsigned char*)(&(x)+1)-((unsigned char*)&(x))
int main(void)
{
int x = 23;
int *p = &x;
printf("%d\n", (int)SIZEOF(*p+ +));
return 0;
}
and running it on a DS9K. But I'm too afraid to do that.
Jul 1 '07 #13
Heinrich Pumpernickel wrote:
<r...@see.sig.i nvalidwrote:
>Heinrich Pumpernickel said:
.... snip ...
>>
>>void main()

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

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 !
Then both the book AND your instructor are wrong, and both need
replacing.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jul 1 '07 #14

"Heinrich Pumpernickel" <la****@linuxma il.orgha scritto nel messaggio
news:11******** **************@ c77g2000hse.goo glegroups.com.. .
>

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 .
#define SIZEOF( var ) sizeof var
Note that I used sizeof, without parentheses, not sizeof().
for extra credit, write another macro, sizeof_t(type), that
returns the size of a type .
Does the requirement for the previous point also apply to this one?
If so, the latter point is impossible. If not, try:
#define sizeof_t sizeof
Jul 1 '07 #15
Army1987 wrote:
>
.... snip ...
>
I've tried
#define SIZEOF(x) ((unsigned char*)(&(x)+1)-((unsigned char*)&(x))
#include <stdio.h>
int main(void)
{
register int x = 23;
printf("%d %d\n", (int)sizeof(x), (int)SIZEOF(x)) ;
return 0;
}
on my DS9K, and something very strange happened...
Doesn't need a DS9K. Taking the address of a register variable is
illegal.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jul 1 '07 #16

"CBFalconer " <cb********@yah oo.comha scritto nel messaggio news:46******** *******@yahoo.c om...
Army1987 wrote:
>>
... snip ...
>>
I've tried
#define SIZEOF(x) ((unsigned char*)(&(x)+1)-((unsigned char*)&(x))
#include <stdio.h>
int main(void)
{
register int x = 23;
printf("%d %d\n", (int)sizeof(x), (int)SIZEOF(x)) ;
return 0;
}
on my DS9K, and something very strange happened...

Doesn't need a DS9K. Taking the address of a register variable is
illegal.
If it wasn't illegal, nothing strange would happen even on a DS9K,
since the DS9K is a conforming hosted implementation.
Jul 1 '07 #17
On Sat, 30 Jun 2007 20:15:03 -0700, in comp.lang.c , Heinrich
Pumpernickel <la****@linuxma il.orgwrote:
>On Jul 1, 5:04 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
>Heinrich Pumpernickel said:
well its just an excersise
OK, but in that case I suggest you write your best effort at solving
the problem, post it here, and ask for assistance. You could also
google for it.
>If your instructor gave you this template, you need a new instructor. In
C, main returns int.
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 !
Then I'm afraid that both your book and your teacher are wrong. The C
Standard REQUIRES main to return an int. Whether you want to return
something or not, you need to at least return 0. Otherwise you are
breaking the rules of C.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jul 1 '07 #18
Mark McIntyre wrote:
Then I'm afraid that both your book and your teacher are wrong. The C
Standard REQUIRES main to return an int.
This is incorrect. The C standard requires implementations to accept main
returning int, but does not prohibit other return types either from being
allowed by implementations or from being used by programs.
Jul 1 '07 #19
Harald van D?k said:
Mark McIntyre wrote:
>Then I'm afraid that both your book and your teacher are wrong. The C
Standard REQUIRES main to return an int.

This is incorrect. The C standard requires implementations to accept
main returning int, but does not prohibit other return types either
from being allowed by implementations or from being used by programs.
True enough, but neither does it define the behaviour of such program.
The C Standard does not forbid implementations from accepting Fortran
programs, either. That doesn't mean that Fortran programs are correct C
programs.

--
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
Jul 1 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2839
by: Tomislav Lepusic | last post by:
Hello, I don't know if this is the right group (I'm more in Perl, know nothing about Python), so if you can help me thanks, if not, sorry to bother you. I'm working on my student project and I'm totally lost and don't have any idea how to continue, and the deadline is 1.9.2004. Please help, 'couse i'm loosing myself in this...
1
1930
by: the_proud_family | last post by:
HELP ME PLEASE!! my email is the_proud_family@yahoo.com I can't get the ball to go up right side and then I need it to turn around and keep turning until velocity=0 I have been at it for the past 2 weeks now i give up and call for help. Please if anyone can gide me through i will be so grateful!! I have pasted my code below
2
628
by: Mj23 | last post by:
I'm Italian and I'm sorry for my English!!!I'm new of this ns and so I don't know if my question is suitable. I have a problem: using the C++ language,the QT library and the OpenGL library realize a CAD that can: a) load a glut three-dimensional model b) load a three-dimensional model in dxf format Thank you very much
0
1631
by: Prasanth U | last post by:
Hi All, We are facing a problem while integrating a HTML help file (chm version 1.x) to our windows .net application (C#). The help topic for the controls in the application are shown using the following command Help.ShowHelp(helpParent, @HelpFile, HelpNavigator.Topic, topicName); The problem is like this. Our application has two modes...
22
2566
by: Rafia Tapia | last post by:
Hi all This is what I have in mind and I will appreciate any suggestions. I am trying to create a xml help system for my application. The schema of the xml file will be <helpsystem> <help id="unique id"> <text>text data</text> <link href="optional tag which will hold a reference to a uri"
3
1707
by: Mitchell Thomas | last post by:
I hope someone out there can solve my mysterious problem. I have tried everything imaginable, even paid $35 to Microsoft to help me, but they were not able to figure out this problem: Here is the problem: I recently created a new database in Access 2002. I took data from an > access 97 database converted one of the tables to access 2002...
2
3285
by: chanchito_cojones | last post by:
hi there, I am needing some help with a database I am putting together. The database works off of a main Form, which then has buttons on it that will open up other forms. The problem I am having is that if I am updating any information on the current record, and then open one of the other forms, the new form will not show the updated...
7
5346
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available from clicking on many of the available topics (mostly methods but some properties are also unavailable). This same problem occurs with many, if not...
23
3241
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application to create certain textboxes, labels, and combo boxes? Any ideas would be appreciated. Thanks
16
2778
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and efficient navigating within Visual Studio 2005. Let's say your project (or solution) has dozens of forms and hundreds or even thousands of routines. ...
0
7449
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7642
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7405
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5950
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5316
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
688
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.