473,908 Members | 4,008 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

check structure member at compile-time

Hello group,

Is it possible to check the presence of a structure member at compile
time using only the C preprocessor?

What I have in mind could look like the following.

Suppose the structures s1 and s2 look like

struct s1 {
int A;
......
int Z;
}

struct s2 {
int B;
......
int Z;
}

Then this code snippet

#if EXISTS(s1, A)
#error struct s has a member A
#else
#error struct s has no member A
#endif

needs to produce the "struct s has a member A" message, while this one

#if EXISTS(s2, A)
#error struct s has a member A
#else
#error struct s has no member A
#endif

needs to produce the "struct s has no member A".

How do I go about defining the macro EXISTS in such case? Is it at all
possible?

Thanks,

Dmitri
Nov 14 '05 #1
1 5388
Dmitri Vorobiev wrote:
Hello group,

Is it possible to check the presence of a structure member at compile
time using only the C preprocessor?


No. The preprocessor does its work on a stream of
tokens, and the recognition of tokens as `if' or `3.14'
or `*=' happens after the preprocessor is finished. In
particular, `struct' is merely a token as far as the
preprocessor is concerned; it isn't recognized as a
keyword until later.

Situations like this are the origin of the saying
"The preprocessor doesn't know C."

--
Er*********@sun .com

Nov 14 '05 #2

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

Similar topics

8
8448
by: Ganesh Kundapur | last post by:
Hi all, struct test { static int x; }X; main() { X.x = 100; }
6
4211
by: Eric Smith | last post by:
Is a structure containing an incomplete array as its last element (per paragraph 2 of section 6.7.2.1 of ISO/IEC 9899:1999 (E)) itself an incomplete type? That appears to be indicated by paragraph 22 of section 6.2.5. If so, that seems to make it difficult to allocate such structures, because sizeof() is not allowed on incomplete types (paragraph 1 of section 6.5.3.4). For instance, I've routinely done things like this: struct foo {...
7
3439
by: Randy Yates | last post by:
Hi, I work in an embedded environment in which we often use a mix of C and assembly code. Thus a recurring requirement is to be able to take a C header file with structure definitions as input and create an assembly include file with the structure member offsets defined. The problem is that the structure offsets are a complicated function of other syntactic elements such as other structures (I could have a structure as a member of...
9
4149
by: B Vidyadhar Joshi | last post by:
I have converted a few C++ structures to C# structures. However, when I use them in the code, I get errors in "internal static BluetoothDeviceInfo Create()". I feel I'm doing something wrong while converting the union. Could someone help please? /* typedef ULONGLONG BTH_ADDR; typedef struct _BLUETOOTH_ADDRESS { union { BTH_ADDR ullLong; // easier to compare again BLUETOOTH_NULL_ADDRESS
3
1770
by: Steve | last post by:
Hello, I created a public Structure in a Standard Module and also an array of Structures. Then I load data into the array of structures in a public sub that I call on the Form load event. Next I have a class, and I want to pass a structure member from my array of Structures to the class constructor. In the form I instantiate a class object. Module1
7
2646
by: Jake Thompson | last post by:
Hello I created a DLL that has a function that is called from my main c program. In my exe I first get a a pointer to the address of the function by using GetProcAddress and on the dll side I make sure that my function is being exported by adding a line to the .def file. This seems to work because when I debug it recognizes the dll and once it hits the function it goes right into the proper location of the dll.
5
13949
by: Steven Woody | last post by:
hi, i get a struct as below, typedef struct { uint16_t id; long offset; } foo_t;
3
391
by: Michael | last post by:
Hi, In some code in my header file I have: typedef struct{ int book_id; char title; char author; int year; int price;
5
3810
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);" I thought that it is very hard to memory map structure array. I need both read and write memory mapped file at both side of C# and C++.
5
6394
by: hnshashi | last post by:
I have writtem kernel(2.4) module to commu. with user space appl. using netlink socket. I am getting compilation error. kernel module:-> #include <linux/skbuff.h> #include<linux/module.h> #include <linux/socket.h> #include <linux/config.h> #include <linux/module.h> #include <linux/kernel.h>
0
10029
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10913
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
11040
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
10536
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
9721
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
8094
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
7244
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
6133
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3354
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.