473,769 Members | 4,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Associate argument with entry in header file

All,

We want to associate a argument with a corresponding entry in header
file. How do we do this?
The argument is defined in a header file. We want to extract the value
of the #define variable.

For example,
_______________ _______________ _______________ _______________ _______________ _____
myheader.h
-----------

#define VARIABLE 10
_______________ _______________ _______________ _______________ _______________ _____

Checkheader.c
_______________ _______________ _______________ _______________ _______________ _____
#include myheader.h

int main(int argv, char * argc){
?????????
}
_______________ _______________ _______________ _______________ _______________ _____

We should be able to run Checkheader VARIABLE and it should return 10.

Thanks in advance,
Shekar
Nov 14 '05 #1
8 1468

"Chandrashe kar Tippur" <ct*****@msn.co m> wrote in message
news:62******** *************** ***@posting.goo gle.com...
All,

We want to associate a argument with a corresponding entry in header
file. How do we do this?
The argument is defined in a header file. We want to extract the value
of the #define variable.

For example,
_______________ _______________ _______________ _______________ _______________ _
____ myheader.h
-----------

#define VARIABLE 10
_______________ _______________ _______________ _______________ _______________ _
____
Checkheader.c
_______________ _______________ _______________ _______________ _______________ _
____ #include myheader.h

int main(int argv, char * argc){
?????????
}
_______________ _______________ _______________ _______________ _______________ _
____
We should be able to run Checkheader VARIABLE and it should return 10.


#include <string.h>

#define VARIABLE 10

int main(int argc, char **argv)
{
return VARIABLE * ((argc > 1) && !strcmp(argv[1], "VARIABLE") );
}
-Mike
Nov 14 '05 #2
Chandrashekar Tippur wrote:

We want to associate a argument with a corresponding entry in
header file. How do we do this? The argument is defined in a
header file. We want to extract the value of the #define variable.

For example,
_______________ _______________ _______________ _______________ _____
myheader.h
-----------

#define VARIABLE 10
_______________ _______________ _______________ _______________ _____

Checkheader.c
_______________ _______________ _______________ _______________ _____
#include myheader.h

int main(int argv, char * argc){
?????????
}
_______________ _______________ _______________ _______________ _____

We should be able to run Checkheader VARIABLE and it should
return 10.


---- File checkheader.c ----
#include <stdio.h>
#include "myheader.h "

int main(void)
{
printf("%d\n", VARIABLE);
return 0;
}

Time to read your C book.

--
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 14 '05 #3
In article <41************ ***@yahoo.com>,
CBFalconer <cb********@wor ldnet.att.net> wrote:
We should be able to run Checkheader VARIABLE and it should
return 10.
[...] printf("%d\n", VARIABLE);


Note that he specifies the #defined variable name as an argument.

I take it he wants to be able to map from strings to preprocessor
variables at run time, which can't be done. He would need a table
of variable names and values.

-- Richard
Nov 14 '05 #4
Mike,

Thanks for replying back.
The VARIABLE can be anything. The bigger picture is that we need to be
able to get system configuration variables using sysconf() function.
This takes long as the argument and returns long. We want to be able
to pass a variable as argument to this function and get the
corresponding value.
Is there a way to use preprocessor directives like:

#ifdef <Variable name>
return sysconf(<variab le name>)

I appreciate the help.

Shekar
#include <string.h>

#define VARIABLE 10

int main(int argc, char **argv)
{
return VARIABLE * ((argc > 1) && !strcmp(argv[1], "VARIABLE") );
}
-Mike

Nov 14 '05 #5
ct*****@msn.com (Chandrashekar Tippur) wrote in message news:<62******* *************** ****@posting.go ogle.com>...
We want to associate a argument with a corresponding entry in header
file. How do we do this?
The argument is defined in a header file. We want to extract the value
of the #define variable.
[snip...]


You cannot accomplish it in C directly. However, I can propose you a
solution: insert a CodeWorker script into your main() function.
CodeWorker is a parsing tool and a source code generation, freeware
available at "http://www.codeworker. org".

Your file, "main.cpp" becomes:
#include <stdio.h>
#include <string.h>
#include "myHeader.h "

int main(int argc, char **argv) {
if (argc != 2) {
printf("Argumen t expected!");
return -1;
}
// this markup announces a CodeWorker script
/*##markup##"gen erate mapping"*//*##script##*/
/*
// parse the file "myHeader.h " with an extended-BNF script
parseAsBNF({
mapping ::=
#ignore(C++) // ignore C-like comments
// looking for every '#define <numeric> <EOL>
[
// jump to the next '#define'
->[
'#' #readIdentifier :"define"
// register the name of the preprocessor definition
#readIdentifier :sDefine
// numeric value ?
#readNumeric:dV alue
#!ignore // stop to ignore whitespaces or comments
['\r']? '\n' // <EOL>
// store the 'define' and the associated value
=> insert this[sDefine] = dValue;
]
]*
;
}, project, "myHeader.h ");
// code generation of a sequence of if/else on the preprocessor
// definitions: display their corresponding numeric value.
foreach i in project {
// the beginning isn't the same if it is the first definition
// or not
if i.first() {
@
@
} else {
@ else @
}
// the test on the value of the command-line argument:
// display it on the console if valid.
@if (strcmp(argv[1], "@i.key()@" ) == 0) {
printf("\"@i.ke y()@\" = @i@");
}@
}
@
@
*/
/*##script##*/
return 0;
}
Type:
codeworker -autoexpand main.cpp -commentbegin /* -commentend */
and it will add the following code just after the last
'/*##script##*/' and just before ' return 0;':
/*##begin##"gene rate mapping"*/
if (strcmp(argv[1], "VARIABLE") == 0) {
printf("\"VARIA BLE\" = 10");
} else if (strcmp(argv[1], "OTHER") == 0) {
printf("\"OTHER \" = 25.5");
}
/*##end##"genera te mapping"*/
with "myHeader.h " being worth:
#ifndef _myHeader_h_
// this one is refused: not a number
#define _myHeader_h_

// this one is retained
#define VARIABLE 10
// this one is refused, because not an integer
#define WRONG "constant string aren't accepted"
// this one is refused, because points to another definition
#define WRONG2 VARIABLE
// this one is accepted too
#define OTHER 25.5

#endif
Regards,

Cedric Lemaire
Nov 14 '05 #6
ct*****@msn.com (Chandrashekar Tippur) wrote in message news:<62******* *************** ****@posting.go ogle.com>...
The VARIABLE can be anything. The bigger picture is that we need to be
able to get system configuration variables using sysconf() function.
This takes long as the argument and returns long. We want to be able
to pass a variable as argument to this function and get the
corresponding value.


OK, so replace '#readNumeric:d Value' by '#readInteger:d Value' in the
BNF script, to accept long only.
Then, in the template-based script, replace 'printf("\"@i.k ey()@\" =
@i@");' by 'printf("syscon f(\"@i.key()@\" ) = %ld",
sysconf(@i.key( )@));'.
You will need to include the C header for 'sysconf()'.

On your example, it will generate:
printf("sysconf (\"VARIABLE@\ ") = %ld", sysconf(VARIABL E));
Regards,

Cedric Lemaire
--
Nov 14 '05 #7
Chandrashekar Tippur said to us:
Mike,

Thanks for replying back.
The VARIABLE can be anything. The bigger picture is that we need to be
able to get system configuration variables using sysconf() function.
This takes long as the argument and returns long. We want to be able
to pass a variable as argument to this function and get the
corresponding value.
Is there a way to use preprocessor directives like:

#ifdef <Variable name>
return sysconf(<variab le name>)

I appreciate the help.

Shekar


Yes, but it would require an outside program such as a perl script.

Without creating an outside program, you could create a table of
configuration variable names and their values:

#define MAX_ARRAY_SIZE 1000
#define TIMEOUT 600
#define ANOTHER_VALUE 17
struct sysconf_value {
const char * name;
long value;
};
struct sysconf_value values [] = {
"max_array_size ", MAX_ARRAY_SIZE,
"timeout", TIMEOUT,
"another_value" , ANOTHER_VALUE,
0, 0,
};

Define sysconf like so:
long sysconf (const char * str) {
int ii;
for (ii = 0; values[ii].name; ii++) {
if (0 == strcmp (str,values[ii].name)) return values[ii].value;
}
fprintf (stderr, "Can't find sysconf: %s\n", str);
exit(1);
}

In main:
int ii = 0;
const char * tests [] = { "max_array_size ", "timeout", "another_value" , 0 };

for (ii = 0; tests[ii]; ii++) {
printf ("name: %s value: %ld\n", tests[ii], sysconf(tests[ii]));
}

HTH

Nov 14 '05 #8
All,

Thanks for all your replies. I will certainly try the code worker
approach. I will update the thread with the results.
Thanks again.

Shekar
Nov 14 '05 #9

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

Similar topics

37
17082
by: Grzegorz Staniak | last post by:
Hello, I'm a newbie Python user, a systems administrator - I've been trying to switch from Perl to Python for administrative tasks - and one thing I cannot understand so far is why I need the special 'self' (or anything esle) argument in class method definitions. I might have missed an explanation in the docs, a quick Google search did not help. Is there somewhere on the web you could direct me to? Thanks,
1
3400
by: knutsample | last post by:
Hello! I'm trying to associate a file extension to my wxPython script so that all I have to do is double click on the file and my python script will load up with the path of that file. For instance, I've associated all .py files to be opened up with emacs.exe. If I double click on a .py file, emacs.exe would open up with that file in its buffer.
6
2381
by: Ashok | last post by:
hi, i want to know how to make a specific type of file open in an application i developed in python when the user clicks on the file.(in windows) for eg. a .txt file when clicked opens in notepad, a .doc file when clicked opens in MS-word. In the same way i want to make a .xyz file open in the application i developed when clicked. thanks in advance for any advice.
0
7796
by: Scott Chang | last post by:
Hi all, I have Microsoft Visual C++ .NET (2002) program that is installed on my Windows XP Professional Operating System PC. I started my project (named HelloMCPP)in the Managed C++ Application and added a DLL in the .h file to my project for printing "Hello from Managed C++!" out in my console. I did "Build" the program 'HelloMCPP' and I got 'Fatal Error LNK1561: entry point must be defined HelloMCPP'. I do not know how to define the...
2
4683
by: S.Creek | last post by:
Does anybody know how to associate an icon with a file extension that windows is not familiar with? thanks
6
17975
by: Prashant Bhuptani | last post by:
Hi Guys, I am trying to use a C++ dll in VB.NET code. I have imported the dll in the following manner: <code> Imports System.Runtime.InteropServices
14
1750
by: AtomicBob | last post by:
I have been given an interesting task: Make a page with a single form field, which is to take a name (first and last), and regardless of case or whether there is a space between the first and last name, when the user hits the submit button it takes them to a URL that has been assigned to that name. Example 1: user enters "Tony Tiger" in the field and hits submit.
3
2785
by: zhang.yongpeng | last post by:
Hello, I met some problems when trying to sort a list that has shared_ptr in it. here is the non-compliable code. test.cpp: /********************* begin of code *************************/ #include <boost/shared_ptr.hpp> #include <list> class T{ // simplified this class to the simplest form public: T(int t):capacity(t){}
4
6339
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59 Notice: Undefined index: args in /home/mattehz/public_html/acssr/trunk/inc_error.php on line 92 Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_error.php on line 92
0
9423
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
10211
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
10045
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
9994
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
8870
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
7408
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
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.