Thread: better way to scanf of a array of structs member?

  1. #1
    Registered User
    Join Date
    Mar 2008

    better way to scanf of a array of structs member?


    I have a matrix held in a text file, and I want it to go into a array of structs, but I can't seem to do it directly:
    take my struct first.
    struct xmatst { double ev; int ei;};
    struct xmatst *in;
    one of the simpler ones :-)

    but. in order to get my values from my textfile into that ev member, I need to create another array of doubles called tmpin and do the usual:
    or(i = 0; i < N; i++)
    		for(j = 0; j < N; j++) {
    			fscanf(fp, "%lf", &tmpin[i*N+j]);
    And then I need to assign the tmpin values to my "real" array in. I tired to not use tmpin, and in the above assignment I used
    but that gave me an "invalid type argument of ->" error, though I thought that was the way you referenced a pointer to a struct member.

    May be the fact that it's an array of structs changes things. If I have to do things this way in C it'll be slower than perl. :-)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    in is a pointer. in[i*n+j] is just a struct (in the same way that arrayname is a pointer*, but arrayname[25] is an int, or whatever).

    *I mean that arrayname, by itself, will be turned into a pointer to the start of the array.

    Edit: Oh, and why don't you just do fscanf(in, "%lf", &(in[i*N+j].ev)) in the first place? Or do you need to have a separate array of just the doubles?
    Last edited by tabstop; 07-17-2008 at 04:06 PM.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    I'm not sure how your file is formatted but if it's just rows of struct data separated by white space, I would do something like this:

    for (i = 0; i<N; i++) {
      int rv = fscanf(fp, "&#37;f %d", &in[i].ev, &in[i].ei);
      if (rv < 2) {
        /** do something about the error **/
    This reads the file and should fill the rows in correctly as long as your matrix is in row major orientation.

    Further reading.
    Last edited by whiteflags; 07-17-2008 at 04:22 PM.

  4. #4
    Registered User
    Join Date
    Mar 2008
    eeek! thanks guys, very fast replies, and spot on too!

    I'm still getting to know structs, and the -> identifier has me a bit confused.

    I should have tried some c zen, and sat back, and just thought about the fundamentals.

    however, sometime I betray and go to perl ... :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Sorting an Array of Structs not working
    By ashcan1979 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2006, 03:07 PM
  3. memory allocation for flexible array member of struct
    By jcarouth in forum C Programming
    Replies: 3
    Last Post: 09-11-2005, 12:19 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Pointer to Array of Structs
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-06-2002, 08:34 AM