Thread: Array of Struct issue

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    3

    Array of Struct issue

    Hello, I am new to C and I have to work with it because of need to use existing code. I went through several sites and this is my last hope. Please advise.


    I have a file that defines a simple struct .. something like:


    Code:
    typedef struct{
      double x;
      double y;
      double z;
    } Point;

    Now, I have an array of type point

    Point setA[anchorSize];

    what I am trying to do but having issues with is as follows:


    Code:
    for(i=0;i<foobar;i++){
       setA[i]={pt1,pt2,pt3};//pt1-pt3 are some double values
    }

    However, that does not work for me. I am not sure what I am doing wrong or if this is even allowed but I get something like this for error:
    remodelProtein.c:261: parse error before '{' token

    This however does compile but it seems silly:


    Code:
    for{blabla}{
       Point tempPt={pt1,pt2,pt3};
       SetA[i]=tempPt;
    }
    Do I have to do it this way? Please advise

    Much Much Much love and thanks for any help you can provide :smile:

  2. #2
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    Check a few lines above the error line. It looks like you forgot a semi-colon at the end of some line and the compiler got confused.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    what about
    Code:
    for(i=0;i<foobar;i++){
       setA[i].x=pt1;
       setA[i].y=pt2;
       setA[i].z=pt3;
    }
    or
    Code:
    Point tempPt={pt1,pt2,pt3};
    for{blabla}{
       SetA[i]=tempPt;
    }
    Kurt

  4. #4
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    Well,
    Code:
    for{}
    shouldn't even compile.

    I think the first variant is the best. I've more readable. Could you point out which line is the one that's actually giving you problems, and some context?
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  5. #5
    Registered User KidA's Avatar
    Join Date
    Nov 2005
    Location
    Ohio, USA
    Posts
    26
    I do not believe you can do the following:

    Code:
    for(i=0;i<foobar;i++){
       setA[i]={pt1,pt2,pt3};//pt1-pt3 are some double values
    }
    ...because that method is only possible for initialization. Since setA[] has already been initialized earlier, you will need to set each field individually, as ZuK wrote.

    Hope this helps...

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I thought that was clear.
    the statement
    Code:
    setA[i]={pt1,pt2,pt3};
    is not valid c.
    This syntax can only be used to initialize variables.
    The above statement is not an initialization. Is an assignement -> syntax error.
    Kurt
    Edit: KidA was a lot faster.

  7. #7
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    >is not valid c.

    I'm never sure wether that can actually be used and I always get confused, because it's not a problem in initiaisers, I sort of expect the compiler to use a temporary variable and then assign it, though that may be C++ (I did C++ before C so some things I'm still confused about).

    But yes, you're right.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    3
    sorry, ok this is a snipped of the code that WORKS

    Code:
    			
    			currAtom=rnaList[RNA_FILE_COUNTER-1].atomList[p];
    			if(q<anchorSize*3){
    				Point temp1={currAtom.x[anchorPos[0]],currAtom.y[anchorPos[0]],currAtom.z[anchorPos[0]]};
    				Point temp2={currAtom.x[anchorPos[1]],currAtom.y[anchorPos[1]],currAtom.z[anchorPos[1]]};
    				Point temp3={currAtom.x[anchorPos[2]],currAtom.y[anchorPos[2]],currAtom.z[anchorPos[2]]};
    				SetA[q+0]=temp1;
    				SetA[q+1]=temp2;
    				SetA[q+2]=temp3;
    				q=q+3;
    			}
    and when this snippet of the code is replaced with the following one I am getting way too many errors:

    Code:
    currAtom=rnaList[RNA_FILE_COUNTER-1].atomList[p];
    			if(q<anchorSize*3){
    				SetA[q+0]={currAtom.x[anchorPos[0]],currAtom.y[anchorPos[0]],currAtom.z[anchorPos[0]]};
    				SetA[q+1]={currAtom.x[anchorPos[1]],currAtom.y[anchorPos[1]],currAtom.z[anchorPos[1]]};
    				SetA[q+2]={currAtom.x[anchorPos[2]],currAtom.y[anchorPos[2]],currAtom.z[anchorPos[2]]};
    				q=q+3;
    			}
    Sec i will try to get a better error report.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    3
    aha zuk and kidA, just saw your post. Ok I guess that kindav sux for me as it will add more lines as I have a lot of this babies. I cant quite make a nice for loop around it as that would interfere with my pattern loops.

    I was hoping some fancy pointer wonders to help me out but I can live with it (:

    thanks guys!!!
    Last edited by B1acksun; 11-30-2005 at 01:42 PM.

  10. #10
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Anonymous temporary structures are not a feature of standard C (at least pre-C99), so the assignment doesn't work. Some compilers support anonymous temporary structures, but I'd not depend on it.

    I'd recommend coding the thing as something like:
    Code:
    typedef struct{
      double x;
      double y;
      double z;
    } Point;
    
    void setPoint(Point *p, double x, double y, double z)
    {
      p->x = x;
      p->y = y;
      p->z = z;
    }
    and in your loop, do something like:
    Code:
    for(i=0;i<foobar;i++)
    {
       setPoint(&setA[i], pt1, pt2, pt3);
    }
    Assuming that your compiler can return structures, you could also write a helper fuction:
    Code:
    Point point(double x, double y, double z)
    {
      Point p = {x, y, z};  // initialization, not assignment
      return p;
    }
    and in your loop do
    Code:
      setA[i] = point(pt1, pt2, pt3);
    This requires structure return, which IIRC is a required feature in C90.

    I hope this helps.
    Insert obnoxious but pithy remark here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 05:07 AM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Writing an array of struct to file
    By stellastarr in forum C Programming
    Replies: 10
    Last Post: 03-25-2006, 06:59 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM