Hello, All!
I'm implementing function parsing config file, which look like this:
# this is comment
port=10000
path=/var/run/dump.pid
....
Declared the type
#define BUFLEN 1024
typedef struct config_s {
char parameter[BUFLEN];
void *value;
} config_t;
....
#define N 10 /* number of parameters in config file */
config_t conf[N];
....
strcpy(conf[0].parameter, "debug");
conf[0].value = value;
....
It's supposed to keep parameter name and it's value, which can be different
type (string or unsigned int), that's why I use 'void*' pointer. But at this
point I faced with the problem of type casting. If I call, for example:
strcpy(conf[5].parameter, "port");
conf[PORT].value = (unsigned short)value;
than get compiler's warning:
config.c:61: warning: cast from pointer to integer of different size
config.c:61: warning: assignment makes pointer from integer without a cast
I'd wish to keep values of different types in my structure. How is better
and correct to fulfil this?
With best regards, Roman Mashak. E-mail: mr*@tusur.ru 11 3885
Roman Mashak wrote: Hello, All!
I'm implementing function parsing config file, which look like this:
# this is comment port=10000 path=/var/run/dump.pid ...
Declared the type
#define BUFLEN 1024 typedef struct config_s { char parameter[BUFLEN]; void *value; } config_t; ... #define N 10 /* number of parameters in config file */ config_t conf[N]; ... strcpy(conf[0].parameter, "debug"); conf[0].value = value; ...
It's supposed to keep parameter name and it's value, which can be different type (string or unsigned int), that's why I use 'void*' pointer. But at this point I faced with the problem of type casting. If I call, for example:
strcpy(conf[5].parameter, "port"); conf[PORT].value = (unsigned short)value;
than get compiler's warning:
config.c:61: warning: cast from pointer to integer of different size config.c:61: warning: assignment makes pointer from integer without a cast
AFAIK, casting any pointer type to (void *) need not be typecasted.
Its automatically casted for you based on the input you provide.
In your case i feel 'value' is NOT a pointer type, hence its cribbing.
Whats the data type of variable 'value' ?
Can you post a minimal code snippet which best describes the problem.
- Ravi
I'd wish to keep values of different types in my structure. How is better and correct to fulfil this?
With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Roman Mashak wrote: Hello, All!
I'm implementing function parsing config file, which look like this:
# this is comment port=10000 path=/var/run/dump.pid ...
Declared the type
#define BUFLEN 1024 typedef struct config_s { char parameter[BUFLEN]; void *value; } config_t;
Consider storing the value as a char* or char array instead of a void*.
Makes life much easier. Just convert the value to the appropriate type
when you want to use it, e.g.
int myval = config_get_int( "port");
const char* path = config_get_stri ng("path");
Bjørn
[snip]
Roman Mashak said: #define BUFLEN 1024 typedef struct config_s { char parameter[BUFLEN]; void *value; } config_t; ... #define N 10 /* number of parameters in config file */ config_t conf[N]; ... strcpy(conf[0].parameter, "debug"); conf[0].value = value; ...
It's supposed to keep parameter name and it's value, which can be different type (string or unsigned int), that's why I use 'void*' pointer.
Simpler: store a string representation of your int, and just strtol it back
to an int when you need to. Much, much simpler than what you are trying to
do, and commensurately more likely to work.
But at this point I faced with the problem of type casting. If I call, for example:
strcpy(conf[5].parameter, "port"); conf[PORT].value = (unsigned short)value;
than get compiler's warning:
config.c:61: warning: cast from pointer to integer of different size config.c:61: warning: assignment makes pointer from integer without a cast
Right. I can't see any good reason for the (unsigned short) cast. It doesn't
help you in any way. Nor, in fact, would any cast at all. You could do
this:
conf[PORT].value = &value;
and that would compile just fine, but it wouldn't solve your problem;
instead, it would litter your program with pointers to an object that is
almost certainly local in scope - a very bad idea (and no, making it global
isn't the answer either; two wrongs don't make a right).
I'd wish to keep values of different types in my structure. How is better and correct to fulfil this?
If you want to get it working, keep it simple.
#define BUFLEN 1024
#define MAXLEN whatever you think is right
typedef struct config_s {
char parameter[BUFLEN];
int valuetype; /* 0 = str, 1 = unsigned int, 2 = double, 3=?... */
char strvalue[MAXLEN];
unsigned int uivalue;
double dvalue;
} config_t;
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Hello, Richard!
You wrote on Mon, 19 Dec 2005 07:09:21 +0000 (UTC):
[skip]
RH> unsigned int uivalue;
RH> double dvalue;
RH> } config_t;
Thanks to everyone for help!
With best regards, Roman Mashak. E-mail: mr*@tusur.ru Richard Heathfield wrote: If you want to get it working, keep it simple.
#define BUFLEN 1024 #define MAXLEN whatever you think is right typedef struct config_s { char parameter[BUFLEN]; int valuetype; /* 0 = str, 1 = unsigned int, 2 = double, 3=?... */ char strvalue[MAXLEN]; unsigned int uivalue; double dvalue; } config_t;
Once doing this, why not put the values in a union? This is the
one of the canonical places for one...
-David
David Resnick said: Once doing this, why not put the values in a union? This is the one of the canonical places for one...
It depends on his needs, of course. It can be useful to be able to associate
more than one value type with a given name.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Richard Heathfield wrote: David Resnick said:
Once doing this, why not put the values in a union? This is the one of the canonical places for one...
It depends on his needs, of course. It can be useful to be able to associate more than one value type with a given name.
Yeah, but OP said
"It's supposed to keep parameter name and it's value, which can be
different
type (string or unsigned int)"
While it might be useful to keep multiple types, he didn't ask for it.
Mind you, I prefer your other suggestion, keep everything as strings
then use accessors (config_get_str ing, config_get_doub le,
config_get_int,
config_get_bool , etc) to decied what to convert the string to. Neater.
-David
Hello, David!
You wrote on 19 Dec 2005 06:49:20 -0800:
DR> "It's supposed to keep parameter name and it's value, which can be
DR> different
DR> type (string or unsigned int)"
DR> While it might be useful to keep multiple types, he didn't ask for it.
DR> Mind you, I prefer your other suggestion, keep everything as strings
DR> then use accessors (config_get_str ing, config_get_doub le,
DR> config_get_int,
DR> config_get_bool , etc) to decied what to convert the string to. Neater.
The method proposed by RH earlier works fine for me. What about using
unions, can you give example to let me understand clearly?
With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Roman Mashak wrote: Hello, David! You wrote on 19 Dec 2005 06:49:20 -0800:
DR> "It's supposed to keep parameter name and it's value, which can be DR> different DR> type (string or unsigned int)"
DR> While it might be useful to keep multiple types, he didn't ask for it. DR> Mind you, I prefer your other suggestion, keep everything as strings DR> then use accessors (config_get_str ing, config_get_doub le, DR> config_get_int, DR> config_get_bool , etc) to decied what to convert the string to. Neater. The method proposed by RH earlier works fine for me. What about using unions, can you give example to let me understand clearly?
With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Instead of doing this:
typedef struct config_s {
char parameter[BUFLEN];
int valuetype; /* 0 = str, 1 = unsigned int, 2 = double, 3=?... */
char strvalue[MAXLEN];
unsigned int uivalue;
double dvalue;
} config_t;
You could do this:
typedef struct config_s {
char parameter[BUFLEN];
int valuetype; /* 0 = str, 1 = unsigned int, 2 = double, 3=?... */
union {
char strvalue[MAXLEN];
unsigned int uivalue;
double dvalue;
} value;
} config_t;
Having it be a union has a few virtues
1) saves memory, which usually isn't a big deal, but could be if you
have a union of lots of types
2) clarifies intent. These values are mutually exclusive. You should
have
exactly one of them.
That said, I think it makes rather more sense to have a config
mechanism
where everything is strings and the accessor determines what the type
is...
-David This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: verec |
last post by:
I just do not understand this error.
Am I misusing dynamic_cast ?
What I want to do is to have a single template construct
(with no optional argument) so that it works for whatever
T I want to use it with. Now, if that T happens to be some
subclass of a known base class (object, in this case), I
want to perform some extra stuff ...
I've read Faq#35, and the most natural solution would have
|
by: john |
last post by:
Hello:
I have a simple program with a function that takes a void * parameter.
I want to be able to distinguish a type once the flow of control is
inside the body of the funtion. I am using conditional right now.
Unfortuneately the conditionals do not adequately filter the the types
that do not apply to the specific call. I could send a flag as a
second parameter, but I would rather the program dynamically recognize
the correct type....
|
by: infobahn |
last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
|
by: Axel Dahmen |
last post by:
Hi,
I want to define a local variable having a type defined somewhere else. Is
this possible in C#?
Here's a more detailed description of what I'm about:
Given a member somewhere in a class:
|
by: Shawn |
last post by:
As if it won't be clear enough from my code, I'm pretty new to C
programming. This code is being compiled with an ANSI-C compatible
compiler for a microcontroller. That part, I believe, will be
irrelavent. My syntax is surely where I am going wrong.
I'd like to be able to call this routine to read different values from
another device. This routine would be called quite simply as follows:
void main()
{
| |
by: Erik de Castro Lopo |
last post by:
Hi all,
The GNU C compiler allows a void pointer to be incremented and
the behaviour is equivalent to incrementing a char pointer.
Is this legal C99 or is this a GNU C extention?
Thanks in advance.
Erik
|
by: planetzoom |
last post by:
Given the following code:
#include <stdio.h>
int main(void)
{
char array = "What is your favorite car?";
void *vp = &array;
printf("%s\n", vp);
|
by: Jon |
last post by:
Hi,
When I try to compile the following generic class, the compiler gives
me many errors "Cannot conver type '...' to '...'" on the lines
indicated. Besides, the C# compiler gives me errors even if I don't
declare and instantiate C_Filter_MA<Ti,Tsin my program.
Ti and Ts should be one of {Byte, UInt16, UInt32, UInt64, SByte,
Int16, Int32, Int64, double}.
Any clue on how to be able to compile this class?
|
by: armanlunix |
last post by:
I have a ListView in which there are items/subitems...what I want is that when I select an entire row, the identifier will be used in WHERE clause in the SELECT mysql command.
But when I do this "Select ID_NUMBER, NAME from students WHERE subjec_code =" & ListView1.SelectedItem...I got an error that it is not allowed to convert to string....
What should I do????
Thanks!
|
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...
|
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,...
| |
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...
|
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,...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |