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


Groups > comp.lang.c > #102943

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

Message-Id <m7j3md-aa6.ln1@walter.foo>
From "Peter \"Shaggy\" Haywood" <phaywood@alphalink.com.au>
Subject Re: Access structure members via pointer to an array of structures
Newsgroups comp.lang.c
Date 2017-01-30 22:04 +1100
References <bfad510c-0f37-4e90-9482-4eb10f68fca3@googlegroups.com>

Show all headers | View raw


Groovy hepcat Alla _ was jivin' in comp.lang.c on Sat, 21 Jan 2017 9:00
pm. It's a cool scene! Dig it.

> Hello!
> 
> Please, help me to find my mistake; it seems I do everything correctly
> but, most likely, I am mistaken.

  You are mistaken.

> I will show only a part of code to explain my issue.

  Usually we prefer you to post the smallest complete, compileable
program that demonstrates the problem. But in this case the problem is
fairly obvious.

> 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, you don't. Please try to think about what you are actually doing,
not merely what you intended.

>                              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.

  What you get is undefined behaviour, because you a) read uninitialised
data which is b) beyond the end of an array, in unknown memory.

> #define DATA_SIZE 5
> typedef struct data {

  ...

>     double sales;

  ...

> } 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;
> 
> Then I pass the pointer dat_years into a function:
> /** Parse numbers **/
> bool numbers(char *line, struct data *dat_years)
> {
>     char *ptr = line;
>     char *not_converted = NULL;
>     int count = 0;
>     
>     while(count < DATA_SIZE)
>     {
>         dat_years->sales = strtod(ptr, &not_converted);

  What are you doing here? Well, I'll explain it to you. You're
accessing dat_years->sales. (More below.)

>         /****** 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); fprintf(stderr, "Loop beg2:
>         sales[%i] %f\n", count, dat_years[count].sales);

  Are you intending to enter the International Obfuscated C Coding
Contest? If not, then you need to lay out your code better so we can
read it more easily. I've taken the liberty to rearrange these lines...

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

  Now, here you're accessing dat_years[count].sales. Now, how does this
differ from what you are doing above, hmmm? Now think about it. How
does dat_years->sales differ from dat_years[count].sales?
  Understand this and you will understand why your code is not working.

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

  Here you are updating dat_years, obviously. However, since you are
accessing dat_years[count].sales above, you're going beyond the end of
the array, into no man's land!

>             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;
> }

  There are other issues here, but I'll leave those for now so you can
concentrate on one problem at a time.

> Thank you!

  You're welcome!

-- 


----- Dig the NEW and IMPROVED news sig!! -----


-------------- Shaggy was here! ---------------
              Ain't I'm a dawg!!

Back to comp.lang.c | Previous | Next — Previous 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