From: Matthew Harrison <matth@3d-computers.co.uk>
To: Mariano Moreyra <moremari@aca.org.ar>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: named structure members
Date: Thu, 11 Sep 2003 16:42:55 +0100 [thread overview]
Message-ID: <20030911154254.GA15184@3d-computers.co.uk> (raw)
In-Reply-To: <000e01c3787a$52480260$0b04a8c0@aca.org.ar>
[-- Attachment #1.1: Type: text/plain, Size: 5442 bytes --]
that's very generous of you.
c is not my strong point yet so all help is appreciated.
attached is the sources in question. most of the code is
in src/inventory.c and src/read_config_var.c
thanks again for taking the time
On Thu, Sep 11, 2003 at 12:35:24PM -0300, Mariano Moreyra wrote:
> Are you still getting that segfault??
> Send me the section of the code where the strcpy is (if you want ofcourse
> hehe)
>
>
> -----Mensaje original-----
> De: Matthew Harrison [mailto:matth@3d-computers.co.uk]
> Enviado el: Jueves, 11 de Septiembre de 2003 12:28
> Para: Mariano Moreyra
> CC: linux-c-programming@vger.kernel.org
> Asunto: Re: named structure members
>
>
> On Thu, Sep 11, 2003 at 11:41:34AM -0300, Mariano Moreyra wrote:
> > Ok... I understood that what you wanted was to name your structure members
> > dynamically.
> > But if you will only have those for members, that's a good approach
> > Now, are you allocating memory for your strings before doing a strcpy??
> > Because you defined your members like char pointers, you have to
> dynamically
> > allocate some memory form them before use.
> > You could define your members like arrays instead of char pointers (i.e
> char
> > db_host[256])
> >
>
> actually that was one of the things i tried to get rid of the segfault.
>
> i used to have the struct below like:
>
> typedef struct _CFG_struct
> {
> char db_host[32];
> ...
> }
>
> but then i changed it to the below option while trying to debug.
>
> >
> > ------
> >
> > this is getting very complex.
> >
> > here is the struct that i want my config to go into:
> >
> > <snip>
> > typedef struct _CFG_struct
> > {
> > char *db_host;
> > char *db_name;
> > char *db_pass;
> > char *db_user;
> > }CFG;
> > </snip>
> >
> > and I am doing the following (pseudo code)
> >
> > <snip>
> > for(i = 0; i <= array_of_config_items; i++)
> > {
> > keyword = array_of_config_items[i];
> >
> > while (read_config_file != EOF)
> > {
> > check_return_value;
> > if keyword = 'item1'
> > {
> > item1 = value;
> > }
> > elsif keyword = 'item2'
> > {
> > item2 = value;
> > }
> > and_so_on();
> > }
> > }
> > </snip>
> >
> > i can sort it now with the multiple if's but now it segfaults on
> > a strcpy for some reason and i'm still learning how to use gdb.
> >
> > On Thu, Sep 11, 2003 at 11:03:59AM -0300, Mariano Moreyra wrote:
> > > > what i'm doing is stepping thru an array of different config options
> > > > calling the read_config_var on each one and checking the output.
> > > > if the output is good then i want to store the value in a member of
> > > > a structure. for example i want the config options to be stored like
> > > > this:
> > >
> > > > config.db_host = 'maiden.genestate.com'
> > > > config.db_user = 'root'
> > >
> > > > you get the idea. my question is how do i dynamically assign a name
> > > > to a structure member. I have good_values[i] which contains the
> > > > current config directive but if you look at line 8 of the example,
> > > > you can see the problem, how do i say config.good_values[i], when
> > > > good_values[i] is not itself a member.
> > >
> > > I think that I get the idea ...
> > > But are you sure that is that what you want??
> > > I mean, the idea is that in your config option you could have any config
> > > directives (or keys) and values??
> > > How you will handle that options later without knowing from the begining
> > > what options you could have??
> > > Anyway...I think that you could do is create your structure like this:
> > >
> > > typedef struct {
> > > char key[255];
> > > cahr value[255];
> > > } t_config;
> > >
> > > t_config config[20]; // Change 20 and 255 to adapt to your needs
> > >
> > >
> > > so your code would look like this:
> > >
> > > for(i = 0; i < n_values; ++i)
> > > {
> > > strcpy(keyword, good_values[i]);
> > >
> > > switch (read_config_var(values_file, keyword, value))
> > > {
> > > case 0:
> > > strcpy(config[i].key, keyword);
> > > strcpy(config[i].value, value);
> > > break;
> > > case -1:
> > > printf("\nFile Error for [%s] \n",
> > > values_file);
> > > break;
> > > case -2:
> > > printf("\nBad User Parm for [%s] \n",
> > > keyword);
> > > break;
> > > default:
> > > printf("\nUnknown Error Occurred \n");
> > > break;
> > > }
> > > }
> > >
> > >
> > > I'm not sure if is that what you want...and if I really understood your
> > > problem...
> > > So, let me know if this mail sucks! :)
> > >
> > > --
> > > Mariano Moreyra <mariano_moreyra@aca.org.ar>
> >
> > --
> > Mat Harrison
> > Technical Developer
> > 3d Computer Systems Ltd.
> > matth@3d-computers.co.uk
>
> --
> Mat Harrison
> Technical Developer
> 3d Computer Systems Ltd.
> matth@3d-computers.co.uk
--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
matth@3d-computers.co.uk
[-- Attachment #1.2: inventory-0.1.tar.gz --]
[-- Type: application/x-tar-gz, Size: 52687 bytes --]
[-- Attachment #2: Type: application/pgp-signature, Size: 187 bytes --]
next prev parent reply other threads:[~2003-09-11 15:42 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-09-11 11:07 named structure members Matthew Harrison
2003-09-11 11:50 ` Jeff Woods
2003-09-11 14:03 ` Mariano Moreyra
2003-09-11 14:26 ` Matthew Harrison
2003-09-11 14:41 ` Mariano Moreyra
2003-09-11 15:28 ` Matthew Harrison
2003-09-11 15:35 ` Mariano Moreyra
2003-09-11 15:42 ` Matthew Harrison [this message]
2003-09-11 15:50 ` Glynn Clements
2003-09-11 16:52 ` Jan-Benedict Glaw
2003-09-11 17:13 ` Matthew Harrison
2003-09-12 13:22 ` Mariano Moreyra
2003-09-12 13:47 ` Matthew Harrison
2003-09-12 13:52 ` Mariano Moreyra
2003-09-12 14:33 ` Matthew Harrison
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20030911154254.GA15184@3d-computers.co.uk \
--to=matth@3d-computers.co.uk \
--cc=linux-c-programming@vger.kernel.org \
--cc=moremari@aca.org.ar \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).