473,582 Members | 3,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

libs and constants

Hello,

I want to build a ".lib" library where I will have some constants.
The constants are not only int, char, but also struct constants.
How do I declare / implement these struct constants? I came up with 3
solutions but I am really satisfied with none of them:

1. use #define
This can work with constants int or char*, but not really with structures.
If you have
| #define my_struct_const ant { { 1, 2 }, 3 }

you can then write
| my_struct constant = my_struct_const ant;
but subsequentely you cannot write
| constant = my_struct_const ant;

2. declare the variable in .h:
| extern my_struct my_struct_const ant;
and implement in a .c:
| my_struct my_struct_const ant = { { 1, 2 }, 3 };
you can then write
| my_struct constant = my_struct_const ant;
| constant = my_struct_const ant;

So far so good.
But I understand however that the initialisation will not be performed...
So I will need to provide an init() function and require the users of the
..lib to call these function...
In addition I will need two lines in two separate files (declaration /
implementation) so it is harder to maintain.

3. provide a make function
| extern my_struct make_my_struct( int, int, int);
and use #define:
| #define my_struct_const ant make_my_struct( 1, 2, 3)
you can then write
| my_struct constant = my_struct_const ant;
| constant = my_struct_const ant;
But this does not scale very well, you need to provide make functions for
all the types you will have constants.

Are there any better options than thses ones?

Thanks in advance for your comments and suggestions!

SerGioGioGio
Nov 14 '05 #1
1 1405
In article <42********@new s.starhub.net.s g> SerGioGio <nospam> wrote:
I want to build a ".lib" library where I will have some constants.
The constants are not only int, char, but also struct constants.


Only C99 has actual support for structure-valued constants.

If all of your compilers support (at least this much) C99, you
can write, e.g.:

#define ZORG ((const struct evil) {{ 1, 2 }, 3 })

(The "const" is not required, but I consider it a good idea.)

Note that the user of this constant must take care to avoid
taking the address of the constant, which does work but may
point to an object with automatic storage duration:

#ifdef OLDWAY
typedef double evil_t;
#define ZORG (42.0)
#else
typedef struct evil evil_t;
struct evil { int b[2]; int c; };
#define ZORG ((const struct evil) {{ 1, 2 }, 3 })
#endif

const evil_t *foo(void) {
const evil_t *p;
...
p = &ZORG;
return p; /* ERROR */
}

One cannot take addresses of, e.g., simple int- and double-valued
constants, so programmers are not particularly *likely* to attempt to
take the address of the constant. If one defines OLDWAY above, the
code fails to compile.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #2

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

Similar topics

2
1513
by: B Squared | last post by:
I recently installed PHP 5.0.4 on my Fedora system. I compiled in the GD option. (--with-gd). Then I tested the ImageTypes() function. It seems .png and .gif (IMG_PNG and IMG_GIF) are supported, but jpeg (IMG_JPEG) is not. Is this expected? How do I get .jpeg support? -- segway -- Out of curiousity, I checked the php-config file in my...
0
3047
by: David W. Fenton | last post by:
Today I was working on a hideous old app that I created a long time ago that does a lot of showing/hiding/resizing of fields on one of the forms. I had used constants to store reference values for many of the top/height/left settings. I noticed that some of the constants were defined by using other constant values, and I was impressed that...
17
2057
by: Neil Cerutti | last post by:
The Glk API (which I'm implementing in native Python code) defines 120 or so constants that users must use. The constants already have fairly long names, e.g., gestalt_Version, evtype_Timer, keycode_PageDown. Calls to Glk functions are thus ugly and tedious. scriptref = glk.fileref_create_by_prompt( glk.fileusage_Transcript |...
0
1100
by: ArtiB | last post by:
Hi, I have VC++ Windows form application in VS 2005. I am not able to access some static libs(VC6 migrated) from this application. Getting exception of type 'System.TypeInitializationException' in Unknown Module This assertion comes before executing the code in Main(). So I want to know are there any issues accessing static...
3
2847
by: Pixel.to.life | last post by:
Hi, Gurus, I recently attempted to build a .Net forms application, that links with old style unmanaged C++ static libs. Of course I had to recompile the static lib projects to link properly with the managed application. My questions are two fold: The managed project uses /clr and /MDd (in debug) options. The
1
1807
by: Raman | last post by:
Hi All, I have two libs (libFirst.a and libSecond.a). Both libs contains a common function func(). Now I want to link an application "app" with these two libs as gcc -o app libFirst.a libSecond.a app.c. It gives( and it should ) multiple declaration error . Is there any
2
2507
by: Leslie Sanford | last post by:
I want to define a set of floating point constants using templates insteand of macros. I'd like to determine whether these constants are floats or doubles at compile time. In my header file, I have this: template<bool DoublePrecision> struct Constants { typedef double SampleType; static const double pi;
54
3574
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header file writing: const double PI = 3.14; Every time using it, include the header file.
1
2510
by: tvnaidu | last post by:
I have windows code to port to Linux, there are some static libraries like xyz.lib, also shared libs ABC.dll, I have to convert those static and shared libs (basically static will be used to make shared) to Linux, what is the extension in Linux (shared is .so, static is .lib?), can I get some sample makefile for static libs and shared libs?....
0
7886
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
7809
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...
1
7920
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
8183
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...
1
5685
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
3809
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
2312
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
1
1413
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1147
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.