2 registered members (alibaba, vicknick),
1,492
guests, and 4
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
typedef and arrays - typedef var myVar[x]
#314004
03/04/10 22:28
03/04/10 22:28
|
Joined: Jun 2004
Posts: 655 to your left
BoH_Havoc
OP
User
|
OP
User
Joined: Jun 2004
Posts: 655
to your left
|
Another day, another problem In C and lite-c i can create a new vartype by writing Now if i want to create a new array var type, in C i would write
typedef var myVar[x]; //x = any number
but in lite-c this doesn't work, i get a syntax error. Any help?
|
|
|
Re: typedef and arrays - typedef var myVar[x]
[Re: BoH_Havoc]
#314010
03/04/10 22:46
03/04/10 22:46
|
Joined: Jan 2003
Posts: 4,615 Cambridge
Joey
Expert
|
Expert
Joined: Jan 2003
Posts: 4,615
Cambridge
|
Yeah, that's how it's supposed to work in C. Have you tried tpyedef var *mypvar;? Just curious, I've currently no A7 installed.
edit: you could still use #defines, but I'm sure you know that.
Last edited by Joey; 03/04/10 22:46.
|
|
|
Re: typedef and arrays - typedef var myVar[x]
[Re: Joey]
#314012
03/04/10 23:09
03/04/10 23:09
|
Joined: Jun 2004
Posts: 655 to your left
BoH_Havoc
OP
User
|
OP
User
Joined: Jun 2004
Posts: 655
to your left
|
Manual:
typedef can be used to redefine variables and structs; it can not be used for arrays and predefined engine objects.
Grrr...i'd have to rewrite 1300 lines of code then. Have you tried tpyedef var *mypvar;? Just curious, I've currently no A7 installed. That somehow works, but i get an error when doing something like this (pointer confusion for the win!):
typedef var *VECTOR3D;
void linCombVECTOR3(VECTOR3D *mresult, const VECTOR3D pos,
const VECTOR3D dir, const var t)
{
mresult[0] = pos[0] + t*dir[0];
mresult[1] = pos[1] + t*dir[1];
mresult[2] = pos[2] + t*dir[2];
}
Syntax error 'can't convert CONV:DOUBLE:POINTER' edit: you could still use #defines, but I'm sure you know that. Nope, how to do it?
|
|
|
Re: typedef and arrays - typedef var myVar[x]
[Re: BoH_Havoc]
#314013
03/04/10 23:18
03/04/10 23:18
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
Expert
|
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
Maybe if you explain what you want, we can offer different solutions? There's things like memory allocation for dynamic arrays...
mresult[0] = pos[0] + t*dir[0];
mresult is a double pointer, first in typedef and then in the arguments, while pos is only in the typedef. You can't make that conversion. (mresult[0])[0] will probably work
Last edited by Joozey; 03/04/10 23:20.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: typedef and arrays - typedef var myVar[x]
[Re: Joozey]
#314014
03/04/10 23:25
03/04/10 23:25
|
Joined: Jun 2004
Posts: 655 to your left
BoH_Havoc
OP
User
|
OP
User
Joined: Jun 2004
Posts: 655
to your left
|
I have 1300 lines of code to generate the matrix for lispSM shadows and i'm trying to convert it to lite-c to spice up my shadows. I'd like to define typedef float VECTOR3D[3]; and typedef VECTOR3D VECTOR3x8D[8]; as they are used all over the code. Now i could rewrite the whole code (change [0] with .x [1] with .y etc, create a struct for the VECTOR3x8D thingy, change code accordingly, etc. pp) or i could try to somehow get this working Even if not using it for the lispSM thing, i'm generally interisted in how to define a new type of array in lite-c as i think this is a nice thing to have [edit] tried it with (mresult[0])[0] and this seems to work (at least i get no errors on compiling). Have to test it a little more. Thanks so far! I'm still interisted in the #define method by Joey though
|
|
|
Re: typedef and arrays - typedef var myVar[x]
[Re: BoH_Havoc]
#314045
03/05/10 10:43
03/05/10 10:43
|
Joined: Jan 2003
Posts: 4,615 Cambridge
Joey
Expert
|
Expert
Joined: Jan 2003
Posts: 4,615
Cambridge
|
(*mresult)[0] = = pos[0] + t*dir[0];
is cleaner, that's how you dereference pointers (although it's basically the same). since c is pretty eager to convert everything correctly, you could try something like this:
typedef float *PVECTOR3D;
#define VECTOR3D float[3]
void myfunc(PVECTOR3D pos)
{
pos[0] = ...
}
...
VECTOR3D mypos;
myfunc(mypos);
what won't work, though, is returning such a VECTOR3D, all it will do is return a pointer to a local variable on the stack which will vanish after the function returns. with typedef'ed variables you'll actually get a copy back. what could work, though, is something like
typedef struct { float x, y, z; } VECTOR3D;
#define _V(x) (float*)(&(x))
VECTOR3D mypos;
_V(mypos)[0] = ...
return mypos;
but then you could as well just use a struct directly. by the way, what you can do is
typedef struct { float[3][8] data; } VECTOR3Dx8D;
VECTOR3Dx8D mymatrix;
mymatrix.data[0][1] = ...
return mymatrix;
or, even more elegant, something like
typedef struct {
union {
float[2][2] data;
struct {
float xx, xy, yx, yy;
};
};
} MAT2x2;
might not work with lite-c, though, unnamed union members don't work in all c compilers.
Last edited by Joey; 03/05/10 10:50. Reason: found something elegant
|
|
|
Re: typedef and arrays - typedef var myVar[x]
[Re: Joey]
#314245
03/06/10 23:12
03/06/10 23:12
|
Joined: Jul 2008
Posts: 1,178 England
MrGuest
Serious User
|
Serious User
Joined: Jul 2008
Posts: 1,178
England
|
but then you could as well just use a struct directly. by the way, what you can do is
typedef struct { float[3][8] data; } VECTOR3Dx8D;
VECTOR3Dx8D mymatrix;
mymatrix.data[0][1] = ...
return mymatrix;
should be?
typedef struct { float data[3][8]; } VECTOR3Dx8D;
|
|
|
|