473,832 Members | 2,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Struct member access with macro

Hai,
Several advanced programs access the struct's member like the one following:

#define str(p) (p.data)
struct node
{
unsigned int data;
};
int main(void)
{
struct node p;
p.data = 10;
printf("%d\n",s tr(p));
return 0;
}

The above is just a basic demo and it is not the only thing around
What is the advantage of doing so?
Nov 13 '05 #1
5 15550
da***********@y ahoo.com wrote:
Several advanced programs access the struct's member like the one following: #define str(p) (p.data)
struct node
{
unsigned int data;
};
int main(void)
{
struct node p;
p.data = 10;
printf("%d\n",s tr(p));
return 0;
} The above is just a basic demo and it is not the only thing around
What is the advantage of doing so?


None I can see at the moment. It just helps obfuscate what the program
is doing. And you should use "%d" with unsigned integers, that's what
"%u" is for. And using "str" is something you should avoid because
names starting with "str" are reserved for the implementation.

Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@p hysik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oe rring
Nov 13 '05 #2
In <a3************ *************@p osting.google.c om> da***********@y ahoo.com writes:
Hai,
Several advanced programs access the struct's member like the one following:

#define str(p) (p.data)
struct node
{
unsigned int data;
};
int main(void)
{
struct node p;
p.data = 10;
printf("%d\n",s tr(p));
return 0;
}

The above is just a basic demo and it is not the only thing around
What is the advantage of doing so?


No real advantage in this example: it saves some typing (if str(p) is
used often enough) at the expense of rendering the code less readable.

The real advantage comes when both the macro definition and the struct
definition come from an external header and the struct is used as an
abstract data type. In this case, you can access a bit of information
stored in the struct, without having any other information about the
struct.

A typical real world example is the Unix fileno(), often implemented as
a macro (because the file descriptor number is usually a member of the
FILE struct).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #3
da***********@y ahoo.com wrote:

Hai,
Several advanced programs access the struct's member like the one following:

#define str(p) (p.data)
struct node
{
unsigned int data;
};
int main(void)
{
struct node p;
p.data = 10;
printf("%d\n",s tr(p));
return 0;
}

The above is just a basic demo and it is not the only thing around
What is the advantage of doing so?


None, in situations as simple as this sample. However,
there can be a benefit in more complex situations. One I ran
across involved a document formatting system that kept track
of a large number of attributes for each paragraph, each word,
and so on. Some of the attributes were used very infrequently,
and it proved worthwhile not to store all of them explicitly
unless they had non-default values. So the code looked like
(*very* inaccurate paraphrase):

struct paragraph {
int this;
int that;
double the_other;
struct rare_attrs *rare;
};

struct rare_attrs {
enum { WEIRD_TYPE, STRANGE_TYPE } attr_type;
int attr_value;
struct rare_attrs *next;
};

#define THIS(p) (p).this
#define THAT(p) (p).that
#define THE_OTHER(p) (p).the_other
#define WEIRD(p) ((p)->rare ? compute_weird(p ) : WEIRD_DEFAULT)
#define STRANGE(p) ((p)->rare ? compute_strange (p) : STRANGE_DEFAULT )

.... and so on. The user of WEIRD and STRANGE didn't need to
know whether these attributes were stored right there in the
paragraph struct or were somewhere in an "exceptiona ls" list
chained from it. Furthermore, if it later turned out that
WEIRD was in actual practice more common than originally
thought, it could be "promoted" into the paragraph struct with
a simple adjustment of the macro, and none of the code that
used it needed to change.

Another use is to hide irrelevant intermediate names, often
in connection with the use of unions:

union flags {
struct {
unsigned int f1 : 1;
unsigned int f2 : 1;
unsigned int f3 : 1;
} s;
unsigned int all_flags;
};

#define F1(p) (p).s.f1
#define F2(p) (p).s.f2
#define F3(p) (p).s.f3
#define ALL(p) (p).all_flags

The intermediate name `s' is just an encumbrance, and could
be done away with if only C would permit anonymous elements (some
compilers support this as a non-conforming extension to the
language). The macros allow the meaningless name to be hidden --
not much of a gain in this simple illustration, but the value
becomes more apparent as the number of such unions within a
single enclosing struct increases.

(By the way, the correspondence between f1/f2/f3 and the bit
positions in all_flags is implementation-defined, and it could
even be the case that some of the flags lie entirely outside
the all_flags element. This technique relies on non-portable
knowledge about the implementation at hand.)

--
Er*********@sun .com
Nov 13 '05 #4
On 29 Sep 2003 06:03:48 -0700, in comp.lang.c ,
da***********@y ahoo.com wrote:
Hai,
Several advanced programs access the struct's member like the one following:

#define str(p) (p.data) ....What is the advantage of doing so?


The only reason for doing this would be if you wanted to hide the
definition of p from the program using it. Its a kind of poor man's
private data. IF you did this, then you'd normally put the definition
of p and the macro in a header.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
Nov 13 '05 #5
Je***********@p hysik.fu-berlin.de wrote:
da***********@y ahoo.com wrote:
Several advanced programs access the struct's member like the
one following:
#define str(p) (p.data)
struct node
{
unsigned int data;
};

.... snip ...
And using "str" is something you should avoid because names
starting with "str" are reserved for the implementation.


Only if followed by a lower case letter. This case is OK.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #6

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

Similar topics

0
2089
by: Alex | last post by:
i have a module in Access which opens an existing Excel file and envokes a macro within the Excel file to draw graphs. now i am trying to convert the Excel macro to an Access one so that the converted Access macro can do the same thing as the Excel macro does when i open the Excel file. Can anybody give me a hint? Thanks in advance! Alex
2
4626
by: hilmilho | last post by:
Hi you, I'm facing a strange problem, and I don't know what to do. Maybe someone could help me understand this? I'm storing IP and Netmask on the struct below. 1st I store IP an print it, its OK, like "127.0.0.1". Then I store Netmask and it somehow messes the IP's last octet, like "127.0.0." ... Check out line comments below, please...
10
4042
by: googler | last post by:
Hello, I have a very simple question that I'm a bit confused about right now. Is it OK to allocate memory by malloc() or calloc() for a struct member and then call free() on it? For example, I have the code below. struct mystruct { int a; char *b; int c; };
0
4876
by: Evgeny | last post by:
I'm trying to run an Access Macro from .NET. My code right now looks like this: Access.ApplicationClass oAccess = new Access.ApplicationClass(); oAccess.Visible = true; string path = @"c:\ttt\Reports.mdb"; app.DoCmd.RunMacro(("Macro1" as Object), (1 as Object), (true as Object));
0
1337
by: Urs Vogel | last post by:
Hi when setting the struct member alignment of a mixed code project to 'default', what is the actual alignment? I need to know since we're creating (struct-) data dynamically and pass it to a third party component which expects a 'default' member alignment. Thanks, Urs
1
2670
by: Sen K via .NET 247 | last post by:
(Type your message here) Hi, Any help is appreciated. I have a situation wherein i need to compact access db after somebusiness logic through VB.NET.I've written a function in VBA tocompact the database and i am calling that function in AccessMacro, and if i run that access macro in the backend, itsworking fine.But when i tried to call that access macro fromVB.NET code,i'm not getting any errors, but that macro is notworking. I'm calling...
4
4664
by: myfavdepo | last post by:
Hi friends, i am having some trouble in my prog. with struct member alignment. I have two different static libraries that i use, each with "struct member alignment" set to 8 bytes. In my application it has to be 2 bytes and when i terminate the my program i am getting this error: DAMAGE: after Normal block (#73) at 0x00323098. When i changed the alignment to 2 bytes for both libraries it run and terminated with out any errors. But one...
5
1763
by: petschy | last post by:
Hello All, I confronted recently with the fact that the size of a struct member can't be determined with sizeof(Struct::Member). I'm aware of the alternative methods to do this, however, I'm interested in the rationale why the Struct::Member syntax is disallowed. Thanks, P
3
2098
Markus
by: Markus | last post by:
Why can I only access a struct member through a pointer to said struct? I thought you were able to access a pointer's members via the dot notation? person mark; person *ptr_mark; ptr_mark->age = 1; mark.age = 1; /** 'age' undeclared?
0
9642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10213
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...
0
9323
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7753
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
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5624
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...
1
4422
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
2
3972
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3078
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.