Page 2 of 2

Re: Give yourself arrays

PostPosted: Wed Sep 28, 2011 12:05 pm
by Fuzzy
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!

Re: Give yourself arrays

PostPosted: Wed Sep 28, 2011 12:51 pm
by Game A Gogo
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;

Re: Give yourself arrays

PostPosted: Thu Sep 29, 2011 3:52 am
by Fuzzy
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

Re: Give yourself arrays

PostPosted: Thu Sep 29, 2011 8:10 am
by lcl
How about this? :D
Code: Select all
stop=(sizeof(array)/sizeof(int))*(stop==CLEAR)+stop*(STOP!=CLEAR)

I think that'd do the same.

Re: Give yourself arrays

PostPosted: Thu Sep 29, 2011 9:20 am
by lcl
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

Re: Give yourself arrays

PostPosted: Thu Sep 29, 2011 11:30 am
by Game A Gogo
This quite inconvenient S: making my code obsolete!

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

Re: Give yourself arrays

PostPosted: Thu Sep 29, 2011 11:51 am
by lcl
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

Re: Give yourself arrays

PostPosted: Thu Sep 29, 2011 2:37 pm
by Fuzzy
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.

Re: Give yourself arrays

PostPosted: Sun May 27, 2012 9:12 pm
by Hblade
Fantastic :D