-
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:
-
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.
-
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
-
Well,
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?
-
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...
-
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. :)
-
>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.
-
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.
-
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!!!
-
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.