Give yourself arrays

You must understand the Game Editor concepts, before post here.

Re: Give yourself arrays

Postby Fuzzy » Wed Sep 28, 2011 12:05 pm

lcl wrote:Actually I wrote that function yesterday and thought it won't work but tested it today, and was
happy to see it did work. I hadn't use pointers for ints before and wasn't sure if it was possible.


It was good design. Its clean and does just one thing. That is a good approach to functions. Keep it up!
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: Give yourself arrays

Postby Game A Gogo » Wed Sep 28, 2011 12:51 pm

Fuzzy wrote:
Code: Select all
#define CLEAR NULL

void resetArray(int * array, int lim)
{
     int i;
     int stop = lim;

     if (stop == CLEAR)
     {
          stop = sizeof(array)/sizeof(int);
     }

    for (i = 0; i < stop; i ++)
    {
        array[i] = 0;
    }
}


Code: Select all
if (stop == CLEAR)
{
     stop = sizeof(array)/sizeof(int);
}


Fuzzy you disappoint me!

Code: Select all
stop=(stop==CLEAR)?sizeof(array/sizeof(int):stop;
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Give yourself arrays

Postby Fuzzy » Thu Sep 29, 2011 3:52 am

Game A Gogo wrote:
Code: Select all
stop=(stop==CLEAR)?sizeof(array/sizeof(int):stop;


Gogo, I disappoint me!

I was going for clarity! Clarity! Well, thats my excuse. :P
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: Give yourself arrays

Postby lcl » Thu Sep 29, 2011 8:10 am

How about this? :D
Code: Select all
stop=(sizeof(array)/sizeof(int))*(stop==CLEAR)+stop*(STOP!=CLEAR)

I think that'd do the same.
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Give yourself arrays

Postby lcl » Thu Sep 29, 2011 9:20 am

It seems that sizeof() can't be used when dealing with pointers..
My sizeof(array)/sizeof(int) says 3, though my array has 50 cells.

Is there a way to avoid this problem?
Or any idea what is wrong because it shows 3?

EDIT: I searched about it and found out that sizeof just can't get the size of the variable pointed by the pointer. :S
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Give yourself arrays

Postby Game A Gogo » Thu Sep 29, 2011 11:30 am

This quite inconvenient S: making my code obsolete!

I wonder if it'd work if you would initiate your array with malloc...
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Give yourself arrays

Postby lcl » Thu Sep 29, 2011 11:51 am

Game A Gogo wrote:This quite inconvenient S: making my code obsolete!

I wonder if it'd work if you would initiate your array with malloc...

Well, I can try that, but I think it won't work.
The problem is that sizeof doesn't know where the pointer is pointing.
That's what I read about it. :P
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Give yourself arrays

Postby Fuzzy » Thu Sep 29, 2011 2:37 pm

Turns out GE has a build in memset function that it takes from c/c++ but we still cant tell the sizeof() a structure pointed at. You'll need to record its size when you create the array.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: Give yourself arrays

Postby Hblade » Sun May 27, 2012 9:12 pm

Fantastic :D
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Previous

Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest

cron