Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #102317
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Optimizing the code: way to reduce if, else if-s within the loop? |
| Date | 2017-01-22 13:16 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <87k29nxmit.fsf@bsb.me.uk> (permalink) |
| References | <a7575c18-3cd0-427c-bfc3-0285b5770cb6@googlegroups.com> <pl678c5imu0sajbmj8lejvlik9hm1snkcg@4ax.com> <2e2405b7-23f8-4572-b71f-f4011e1bc284@googlegroups.com> |
Alla _ <modelling.data@gmail.com> writes:
<snip>
> Below are a few issues I face when trying to implement the offsetof():
I'll explain the problem but you really should do this another way.
<snip>
> Here is the struct's typedef (it's before the main function):
> typedef struct data {
> int year;
> double sales;
> double cs_cogs;
> double cs_gross_profit;
> double cs_sga;
> double cs_oper_inc;
> double cs_dep_amor;
> double cs_oper_prof;
> double cs_int_exp;
> double cs_gain_loss;
> double cs_pretax_inc;
> double cs_tax_exp;
> double cs_net_inc;
> } data;
<snip>
> /** Function to determine a particular struct member **/
> int find_struct_member(char *name)
> {
> int offset;
> if(!strcmp(name, "Sales"))
> {
> offset = offsetof(struct data, sales);
> fprintf(stderr, "offset sales %i\n", offset);
> }
> else if (!strcmp(name, "Cost of Goods Sold") ||
> !strcmp(name, "COGS") )
> {
> offset = offsetof(struct data, cs_cogs);
> fprintf(stderr, "offset cs_cogs %i\n", offset);
> }
> else if(!strcmp(name, "Gross Profit"))
> {
> offset = offsetof(data, cs_gross_profit);
> fprintf(stderr, "offset cs_gross_profit %i\n", offset);
> }
> else if (!strcmp(name, "\"Selling, General, and Admin Exp\"") ||
> !strcmp(name, "SG&A") )
> {
> offset = offsetof(data, cs_sga);
> fprintf(stderr, "offset cs_sga %i\n", offset);
> }
> else if (!strcmp(name, "Operating Income before Depr") ||
> !strcmp(name, "Operating Income before Depreciation") )
> {
> offset = offsetof(data, cs_oper_inc);
> fprintf(stderr, "offset cs_oper_inc %i\n", offset);
> }
> else if(!strcmp(name, "Depreciation and Amortization"))
> {
> offset = offsetof(data, cs_dep_amor);
> fprintf(stderr, "offset cs_dep_amor %i\n", offset);
> }
> else if(!strcmp(name, "Operating Profit"))
> {
> offset = offsetof(data, cs_oper_prof);
> fprintf(stderr, "offset cs_oper_prof %i\n", offset);
> }
> else if(!strcmp(name, "Interest Expense"))
> {
> offset = offsetof(data, cs_int_exp);
> fprintf(stderr, "offset cs_int_exp %i\n", offset);
> }
> else if(!strcmp(name, "Other Gains and Losses"))
> {
> offset = offsetof(data, cs_gain_loss);
> fprintf(stderr, "offset cs_gain_loss %i\n", offset);
> }
> else if(!strcmp(name, "Pretax Income"))
> {
> offset = offsetof(data, cs_pretax_inc);
> fprintf(stderr, "offset cs_pretax_inc %i\n", offset);
> }
> else if(!strcmp(name, "Income Tax Expense"))
> {
> offset = offsetof(data, cs_tax_exp);
> fprintf(stderr, "offset cs_tax_exp %i\n", offset);
> }
> else if(!strcmp(name, "Net Income"))
> {
> offset = offsetof(data, cs_net_inc);
> fprintf(stderr, "offset cs_net_inc %i\n", offset);
> }
> else
> return -1;
> return offset;
> }
This is the sort of ode that should be setting of alarm bells.
Computers should do the repetitive things not programmers. These values
should be an array with named indexes (as per my other post).
<snip>
> /** Function to parse numbers from each line **/
> bool parse_numbers(char *line, int offset, data *dat_years)
> {
> char *ptr = line;
> char *not_converted = ptr;
> int count = 0;
>
> //DEBUG
> fprintf(stderr, "Offset passed to function %i\n", offset);
>
> while(count < DATA_SIZE)
> {
> *((char*)dat_years + offset) = strtod(ptr, ¬_converted);
You are trying to to store a double in a char. You can get most
compiler to warn you that this conversion is likely to loose a lot of
information.
<snip>
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-21 08:52 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-21 09:35 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Barry Schwarz <schwarzb@dqel.com> - 2017-01-21 10:28 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-21 23:49 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-22 00:29 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-22 10:54 +0000
Re: Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-22 03:28 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Keith Thompson <kst-u@mib.org> - 2017-01-22 07:44 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-22 08:24 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Barry Schwarz <schwarzb@dqel.com> - 2017-01-22 10:43 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Keith Thompson <kst-u@mib.org> - 2017-01-22 12:22 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? BartC <bc@freeuk.com> - 2017-01-22 11:33 +0000
Re: Optimizing the code: way to reduce if, else if-s within the loop? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-22 13:23 +0000
Re: Optimizing the code: way to reduce if, else if-s within the loop? BartC <bc@freeuk.com> - 2017-01-22 13:28 +0000
Re: Optimizing the code: way to reduce if, else if-s within the loop? Keith Thompson <kst-u@mib.org> - 2017-01-22 07:47 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? BartC <bc@freeuk.com> - 2017-01-22 16:00 +0000
Re: Optimizing the code: way to reduce if, else if-s within the loop? David Brown <david.brown@hesbynett.no> - 2017-01-22 18:23 +0100
Re: Optimizing the code: way to reduce if, else if-s within the loop? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-01-22 13:47 -0500
Re: Optimizing the code: way to reduce if, else if-s within the loop? Barry Schwarz <schwarzb@dqel.com> - 2017-01-22 10:43 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-01-22 13:53 -0500
Re: Optimizing the code: way to reduce if, else if-s within the loop? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-22 18:57 +0000
Re: Optimizing the code: way to reduce if, else if-s within the loop? BartC <bc@freeuk.com> - 2017-01-22 19:18 +0000
Re: Optimizing the code: way to reduce if, else if-s within the loop? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-22 21:54 +0000
Re: Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-23 02:41 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-23 11:20 +0000
Re: Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-23 03:53 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-23 12:12 +0000
Re: Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-22 03:24 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-22 13:16 +0000
Re: Optimizing the code: way to reduce if, else if-s within the loop? Barry Schwarz <schwarzb@dqel.com> - 2017-01-22 07:47 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-22 08:16 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-01-22 11:54 -0500
Re: Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-22 11:23 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Barry Schwarz <schwarzb@dqel.com> - 2017-01-22 11:08 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-22 22:19 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-22 22:41 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Robert Wessel <robertwessel2@yahoo.com> - 2017-01-23 02:17 -0600
Re: Optimizing the code: way to reduce if, else if-s within the loop? Barry Schwarz <schwarzb@dqel.com> - 2017-01-23 02:16 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-23 03:59 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Keith Thompson <kst-u@mib.org> - 2017-01-23 08:44 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-22 08:23 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Barry Schwarz <schwarzb@dqel.com> - 2017-01-22 07:52 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-22 08:17 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Richard Heathfield <rjh@cpax.org.uk> - 2017-01-23 17:11 +0000
Re: Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-23 10:35 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-23 10:38 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? BartC <bc@freeuk.com> - 2017-01-23 19:06 +0000
Re: Optimizing the code: way to reduce if, else if-s within the loop? Barry Schwarz <schwarzb@dqel.com> - 2017-01-23 14:31 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-23 21:31 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Alla _ <modelling.data@gmail.com> - 2017-01-24 01:56 -0800
Re: Optimizing the code: way to reduce if, else if-s within the loop? Manfred <noname@invalid.add> - 2017-01-24 14:38 +0100
Re: Optimizing the code: way to reduce if, else if-s within the loop? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-24 13:57 +0000
Re: Optimizing the code: way to reduce if, else if-s within the loop? BartC <bc@freeuk.com> - 2017-01-23 12:38 +0000
Re: Optimizing the code: way to reduce if, else if-s within the loop? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-23 15:08 +0000
Re: Optimizing the code: way to reduce if, else if-s within the loop? m.labanowicz@gmail.com - 2017-01-23 07:08 -0800
csiph-web