2 registered members (3s05bmmc, flink),
650
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: 'Structured' programming in Lite-C
[Re: GorNaKosh]
#278936
07/16/09 08:58
07/16/09 08:58
|
Joined: Aug 2007
Posts: 1,922 Schweiz
Widi
Serious User
|
Serious User
Joined: Aug 2007
Posts: 1,922
Schweiz
|
I have a question about allocate Memory with: malloc(sizeof(STRUCT));
typedef struct
{
var koor_x_abs;
var koor_y_abs;
var koor_x;
var koor_y;
var bombenreichweite;
var erzeuger_id;
var kicker_id;
var speed;
PANEL* panel;
} BOMBE;
typedef struct
{
var koor_x_abs;
var koor_y_abs;
var koor_x;
var koor_y;
var start_koor_x;
var start_koor_y;
var anz_leben;
var unverwundbar;
var richtung;
var bombenanzahl;
var bombenanzahl_max;
var bombenreichweite;
var speed;
var speed_orginal;
var anz_schuhe;
var tod;
var id;
var ki;
var sp_status;
var negativ;
var r;
var l;
var u;
var o;
var t1;
var t2;
var joy1_aktiv;
var joy2_aktiv;
var t1_gedr;
var t2_gedr;
PANEL* panel;
BOMBE Bombe[Anz_bomben];
} PLAYER;
PLAYER Player[Anz_spieler];
You can see i want to use a array from the Struct (Anz_spieler = 8). How do i use now malloc? Player = malloc(sizeof(PLAYER)); --> is this right?
|
|
|
Re: 'Structured' programming in Lite-C
[Re: Widi]
#278966
07/16/09 09:59
07/16/09 09:59
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
OP
Expert
|
OP
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
You can create dynamic arrays this way:
PLAYER *player = malloc( sizeof( PLAYER ) * Anz_spieler );
A multidimensional array would go like:
PLAYER **player = malloc( sizeof( PLAYER* ) * players_y );
for( i=0; i<players_x; i++ ) {
player[i] = malloc( sizeof( PLAYER ) );
}
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: 'Structured' programming in Lite-C
[Re: Widi]
#280749
07/24/09 03:08
07/24/09 03:08
|
Joined: Apr 2009
Posts: 39
TrQuocAn
Newbie
|
Newbie
Joined: Apr 2009
Posts: 39
|
excuseme ! But can we use the Array without malloc function ?? for ex :
PLAYER Player[Anz_spieler];
// Not use the malloc here because Player is a array not pointer !
Player[1].r = 2;
Player[3].u = 3;
....
|
|
|
Re: 'Structured' programming in Lite-C
[Re: TrQuocAn]
#280796
07/24/09 09:19
07/24/09 09:19
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
I think so, but "Anz_spieler" cannot be a variable, it must be an actual number, or a '#define'ed constant number.
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: 'Structured' programming in Lite-C
[Re: EvilSOB]
#280875
07/24/09 13:48
07/24/09 13:48
|
Joined: Jun 2006
Posts: 2,640 Earth
Germanunkol
Expert
|
Expert
Joined: Jun 2006
Posts: 2,640
Earth
|
Funny, I thought of pretty much the same method (creating create_ and remove_ functions for all structs) the other day. Good to know someone has tested it!
Joozey, what's the " ** " in your last post? I've never seen that... what does it mean?
~"I never let school interfere with my education"~ -Mark Twain
|
|
|
Re: 'Structured' programming in Lite-C
[Re: Germanunkol]
#280876
07/24/09 13:57
07/24/09 13:57
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Pointer to a pointer. (The start of an array of of pointers) Cause the ENTITYs in the array are really only a pointer to an object, you need an array of pointers.
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: 'Structured' programming in Lite-C
[Re: EvilSOB]
#281025
07/25/09 01:21
07/25/09 01:21
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
OP
Expert
|
OP
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
Funny, I thought of pretty much the same method (creating create_ and remove_ functions for all structs) the other day. Good to know someone has tested it! And good to know someone else has got the same idea ^^ must be some use in this way of programming then! . Joozey, what's the " ** " in your last post? I've never seen that... what does it mean? As evil says. *A = [0][1][2][3][4][5][6] //array, where pointer A points to [0] **B = [A[0]][A[0]][A[0]] //array where pointer A points to [p[0]] A is a normal array with 6 indexes containing some value. B is a normal array with 3 indexes, but each containing a pointer to array A. Doesn't need to be the same array, just a new array of type A. So, B is a pointer to an array containing pointers to an array. Hence the two asteriskes. The compiler will puke with only one asterisk.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: 'Structured' programming in Lite-C
[Re: Joozey]
#281247
07/26/09 12:39
07/26/09 12:39
|
Joined: Sep 2003
Posts: 5,900 Bielefeld, Germany
Pappenheimer
Senior Expert
|
Senior Expert
Joined: Sep 2003
Posts: 5,900
Bielefeld, Germany
|
Joozey, you're explanations are very appreciated! I have an additional question though, hope you don't mind. I'm still thinking in ways of c-script how to solve a certain task. I read your codes and explanations the third time, and I think my problem is that I think more 'material', as you might know or not, I programed a fish game where the player collects fishes in a swarm to keep an evil fish in a safe distance. This 'collecting' might be similar to that of an inventory. At the beginning of a level, I place the fishes in the level, the swarm gets an array to save the handle of each fish that has been collected while the fish saves in a skill the number of its place in the array, and when the fish leaves the swarm, because it dies or it flees because the evil fish comes to near to it, it resets it place in the array to zero, and its skill as well.
What advantage has a struct as you use it in your example to the way that I just described?
|
|
|
|