Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #102282

Re: Access structure members via pointer to an array of structures

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: Access structure members via pointer to an array of structures
Date 2017-01-21 17:59 +0100
Organization A noiseless patient Spider
Message-ID <o603v2$udn$1@dont-email.me> (permalink)
References <bfad510c-0f37-4e90-9482-4eb10f68fca3@googlegroups.com>

Show all headers | View raw


On 21/01/17 11:00, Alla _ wrote:
> Hello!
>
> Please, help me to find my mistake; it seems I do everything correctly
> but, most likely, I am mistaken.
>
> I will show only a part of code to explain my issue.
> I have declared an array of structures and a pointer to
> that array. Then I use the pointer to populate, for now,
> one member of each structure. No problems here: values
> land in their correct places. But when I try to print out
> those values using a pointer, I don't get those values,
> only zeros. Please, see the code for clarification.
>
> #define DATA_SIZE 5
> typedef struct data {
>     int    year;
>     double sales;
>     int    cs_cogs;
>     int    cs_gross_profit;
>     int    cs_sga;
>     int    cs_oper_inc;
>     int    cs_dep_amor;
>     int    cs_oper_prof;
>     int    cs_int_exp;
>     int    cs_gain_loss;
>     int    cs_pretax_inc;
>     int    cs_tax_exp;
>     int    cs_net_inc;
> } data;
>
> in the main function I have this declaration
> (I will later use malloc here, not now):
>
> data years[DATA_SIZE];
> data *dat_years = years;
>

It's not a good idea to make a global variable like this initialised at 
the start (before main() starts) and then change it during your 
"numbers" function.  It means that "numbers" will only work once in the 
lifetime of the program.  But in this case, you are mixing up the global 
variable and the functions parameter - it is bound to cause confusion.

> Then I pass the pointer dat_years into a function:
> /** Parse numbers **/
> bool numbers(char *line, struct data *dat_years)

Don't use the same names for a parameter and a global variable.

And try to be consistent with your type names.  If you want to call your 
struct "data" using a typedef, that's fine.  If you want to call it 
"struct data" to emphasis that it's a struct, that's also fine.  Both 
methods are common.  But try to stick to one method.

> {
>     char *ptr = line;
>     char *not_converted = NULL;
>     int count = 0;
>
>     while(count < DATA_SIZE)
>     {
>         dat_years->sales = strtod(ptr, &not_converted);
>
>         /****** Comment only for explanatory purpose (not part of my code):
>        here I try to print out values; the first fprintf with dat_years->sales
>        works perfectly because it is a standard access to a member via
>        a pointer; but the second one doesn't: I wonder why?
>
> dat_years[count].sales equals the correct number only at the beginning,
> when count = 0; as soon as I increment the count and move pointer to
> the next address dat_years[count].sales shows me only zeros as its values:
> Loop beg: sales num 1062.000000; count 0
> Loop beg2: sales[0] 1062.000000
>
> Loop beg: sales num 1252.000000; count 1
> Loop beg2: sales[1] 0.000000
>
> Loop beg: sales num 1587.000000; count 2
> Loop beg2: sales[2] 0.000000
>
> etc.
> But this member in each structure has the correct value after this
> function, and when the main function calls
> fprintf(stderr, "Main fun: sales[%i] num %f; sales[0] num %f;\n", DATA_SIZE-1, years[DATA_SIZE-1].sales, years[0].sales);
> it produces a correct output:
> Main fun: sales[4] num 2519.000000; sales[0] num 1062.000000;
>
> ******/
>
>         fprintf(stderr, "Loop beg: sales num %f; count %i\n", dat_years->sales, count);

Here, "sales" refers to the "data" object pointed to by dat_years, and 
will loop through the data as dat_years gets incremented each round.

>         fprintf(stderr, "Loop beg2: sales[%i] %f\n", count, dat_years[count].sales);

Here, "sales" refers to the "data" object pointed to by dat_years plus 
an offset of "count" items.  This will step through twice as fast.

>
>         if (not_converted > ptr)
>         {
>             count++;
>             dat_years += (count != DATA_SIZE);

You don't need the "(count != DATA_SIZE)" bit.  It's fine to make your 
pointer go to the address just beyond the end of the data.  It's just 
not okay to try to de-reference such pointers.  So use the far simpler 
and clearer "dat_years++;".

>             ptr = not_converted + (*not_converted != '\0');
>         }
>         else
>         {
>             fprintf(stderr, "Incorrect data on line %s at count %i\n", line, count);
>             return false;
>         }
>     }
>     return count == DATA_SIZE;
> }
>
> Thank you!
>

Your main problem is mixing your array syntax and pointer syntax.

As a general rule, if you have an array of data and it is reasonably 
"natural" to access it as an array, then do that as preference to using 
a pointer or using pointer syntax.  It can make the code's intention 
clearer to you (the programmer), to your reader, and to your compiler. 
(Making code clearer to the compiler leads to better static error 
checking, and more efficient object code.)

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Access structure members via pointer to an array of structures Alla _ <modelling.data@gmail.com> - 2017-01-21 02:00 -0800
  Re: Access structure members via pointer to an array of structures Alla _ <modelling.data@gmail.com> - 2017-01-21 02:08 -0800
    Re: Access structure members via pointer to an array of structures Chad <cdalten@gmail.com> - 2017-01-21 07:36 -0800
      Re: Access structure members via pointer to an array of structures Jerry Stuckle <jstucklex@attglobal.net> - 2017-01-21 11:13 -0500
    Re: Access structure members via pointer to an array of structures Jerry Stuckle <jstucklex@attglobal.net> - 2017-01-21 11:08 -0500
      Re: Access structure members via pointer to an array of structures Chad <cdalten@gmail.com> - 2017-01-21 09:09 -0800
        Re: Access structure members via pointer to an array of structures Jerry Stuckle <jstucklex@attglobal.net> - 2017-01-21 14:32 -0500
          Re: Access structure members via pointer to an array of structures Chad <cdalten@gmail.com> - 2017-01-21 12:57 -0800
            Re: Access structure members via pointer to an array of structures Chad <cdalten@gmail.com> - 2017-01-21 13:04 -0800
              Re: Access structure members via pointer to an array of structures Jerry Stuckle <jstucklex@attglobal.net> - 2017-01-21 17:59 -0500
              Re: Access structure members via pointer to an array of structures cross@spitfire.i.gajendra.net (Dan Cross) - 2017-01-22 14:02 +0000
                Re: Access structure members via pointer to an array of structures raltbos@xs4all.nl (Richard Bos) - 2017-01-22 14:17 +0000
                Re: Access structure members via pointer to an array of structures Tim Rentsch <txr@alumni.caltech.edu> - 2017-01-30 05:25 -0800
            Re: Access structure members via pointer to an array of structures Jerry Stuckle <jstucklex@attglobal.net> - 2017-01-21 17:57 -0500
  Re: Access structure members via pointer to an array of structures Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-01-21 08:09 -0800
    Re: Access structure members via pointer to an array of structures Keith Thompson <kst-u@mib.org> - 2017-01-21 15:15 -0800
      Re: Access structure members via pointer to an array of structures Gareth Owen <gwowen@gmail.com> - 2017-01-22 08:27 +0000
        Re: Access structure members via pointer to an array of structures Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-22 11:17 +0000
          Re: Access structure members via pointer to an array of structures BartC <bc@freeuk.com> - 2017-01-22 11:44 +0000
            Re: Access structure members via pointer to an array of structures Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-01-22 04:27 -0800
              Re: Access structure members via pointer to an array of structures Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-22 13:32 +0000
              Re: Access structure members via pointer to an array of structures Keith Thompson <kst-u@mib.org> - 2017-01-22 07:54 -0800
            Re: Access structure members via pointer to an array of structures Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-22 13:31 +0000
          Re: Access structure members via pointer to an array of structures Gareth Owen <gwowen@gmail.com> - 2017-01-22 14:02 +0000
        Re: Access structure members via pointer to an array of structures Keith Thompson <kst-u@mib.org> - 2017-01-22 07:50 -0800
  Re: Access structure members via pointer to an array of structures Manfred <noname@invalid.add> - 2017-01-21 17:15 +0100
  Re: Access structure members via pointer to an array of structures David Brown <david.brown@hesbynett.no> - 2017-01-21 17:59 +0100
    Re: Access structure members via pointer to an array of structures Alla _ <modelling.data@gmail.com> - 2017-01-21 09:09 -0800
      Re: Access structure members via pointer to an array of structures Barry Schwarz <schwarzb@dqel.com> - 2017-01-21 09:27 -0800
        Re: Access structure members via pointer to an array of structures Alla _ <modelling.data@gmail.com> - 2017-01-21 09:30 -0800
    Re: Access structure members via pointer to an array of structures Alla _ <modelling.data@gmail.com> - 2017-01-21 09:25 -0800
  Re: Access structure members via pointer to an array of structures "Peter \"Shaggy\" Haywood" <phaywood@alphalink.com.au> - 2017-01-30 22:04 +1100

csiph-web