473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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_constant { { 1, 2 }, 3 }

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

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

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_constant make_my_struct(1, 2, 3)
you can then write
| my_struct constant = my_struct_constant;
| constant = my_struct_constant;
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 1396
In article <42********@news.starhub.net.sg> 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
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,...
0
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...
17
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,...
0
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...
3
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...
1
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...
2
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...
54
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...

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.