473,804 Members | 3,464 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 #1
83 2682
Heinrich Pumpernickel said:
28b (macros). write a macro, sizeof(var), that returns the size of
its argument .
#define SIZEOF(x) (sizeof(x))
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>
>
void main()
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
Jul 1 '07 #2
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
>
given is the following template....... ...

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

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

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>
void main()

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

i dont believe that .
Then find out for yourself, by reading the relevant language definition
specification, ISO/IEC 9899.
my c/c++ teacher (very experienced programmer)
Perhaps he is, but he doesn't know much about C if he can't even get the
entry point right.
AND the book were using both say that if main does not return a
value it can be declared void !
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
Jul 1 '07 #4
Heinrich Pumpernickel wrote:
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 !
As it happens, both your teacher and the author of your book are
incompetent. In both C and C++ (there is no such beast as c/c++) main
in a hosted implementation returns an int. Your book should be burnt.
Your "very experienced" teacher is probably protected from burning. It
is time for you to start learning C (or C++) and not what errors your
teacher is "very experienced" in using.
Jul 1 '07 #5
On Jul 1, 8:23 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
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))
#define SIZEOF(x) (&x + 1) -(&x)
It will not work?
Please correct me If I'm wrong.
>
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>
void main()
If your instructor gave you this template, you need a new instructor.
In C, main returns int.
i dont believe that .

Then find out for yourself, by reading the relevant language definition
specification, ISO/IEC 9899.
my c/c++ teacher (very experienced programmer)

Perhaps he is, but he doesn't know much about C if he can't even get the
entry point right.
AND the book were using both say that if main does not return a
value it can be declared void !

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- Hide quoted text -

- Show quoted text -

Jul 1 '07 #6
On Jun 30, 8:15 pm, Heinrich Pumpernickel <lan...@linuxma il.org>
wrote:
On Jul 1, 5:04 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
Heinrich Pumpernickel said:
given is the following template....... ...
void main()
If your instructor gave you this template, you need a new instructor. In
C, main returns int.

i dont believe that .
Well, bully for you. Lots of people refuse to believe facts, but that
doesn't alter the facts.
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 you need to change your books as well as your teacher. C is well
defined by ISO Standards; it doesn't matter what nonsense is contained
in books which claims to be about C.

Jul 1 '07 #7
deepak wrote:
>>On Jul 1, 5:04 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
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))
#define SIZEOF(x) (&x + 1) -(&x)
It will not work?
Please correct me If I'm wrong.
No. Apart from the fact that your macro would still only work for
objects, as Richard already pointed out, it would also always evaluate
to 1. This is a consequence of the way pointer arithmetics work
(semantically, the subtraction of pointers gives you the number of
objects of the type of x which could fit between those two pointers).
sizeof returns the size of the object in bytes and since 'byte' and
'char' are synonyms in C, subtracting char pointers evaluates to the
number of bytes which could fit between them (in this case, the size of
the object).

--
Denis Kasak
Jul 1 '07 #8
J. J. Farrell wrote, On 01/07/07 07:31:
On Jun 30, 8:15 pm, Heinrich Pumpernickel <lan...@linuxma il.org>
wrote:
>On Jul 1, 5:04 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
>>Heinrich Pumpernickel said:
given is the following template....... ...
void main()
If your instructor gave you this template, you need a new instructor. In
C, main returns int.
i dont believe that .

Well, bully for you. Lots of people refuse to believe facts, but that
doesn't alter the facts.
It is understandable the a student would be more inclined to be believe
his teacher and text book than a random collection of people on the
internet.
>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 you need to change your books as well as your teacher. C is well
defined by ISO Standards; it doesn't matter what nonsense is contained
in books which claims to be about C.
True. Also checking the relevant section of the comp.lang.c FAQ at
http://c-faq.com specifically http://c-faq.com/ansi/voidmain.html

Note that the further reading page includes a link to a post by
P.J.Plauger who is an author of a book on C, an author of standard C
libraries, and I think was on the committee that standardised C, so he
knows what he speaks of.

You can look at drafts of the C standards yourself, follow this link for
more details http://clc-wiki.net/wiki/c_standard
--
Flash Gordon
Jul 1 '07 #9

"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...
Jul 1 '07 #10

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

Similar topics

2
2855
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 XML/SOAP/RPC/WSDL things......
1
1957
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
1647
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 of operation 1) Wizard mode - a new file is created by the wizard.
22
2620
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
1720
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 and then > imported it into a new table in access 2002. but for some strange > reason, every once...
2
3308
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 information. I realize that a simple macro solves this problem by basically closing the first form,...
7
5398
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 most, keywords. Is there any way I can activate these "missing" help topics? HELP!
23
3294
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
2824
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. Two Questions: 1) BUILT-IN to Visual Studio 2005. What ideas do you have to quickly
0
10562
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10303
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10070
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7608
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5508
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2978
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.