Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/20/24 01:28
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 645 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
count of array elements #430807
10/02/13 16:14
10/02/13 16:14
Joined: Aug 2002
Posts: 3,258
Mainz
oliver2s Offline OP
Expert
oliver2s  Offline OP
Expert

Joined: Aug 2002
Posts: 3,258
Mainz
Is there a way to find out the count of elements of an array?

Code:
var var_tmp[5];
ENTITY* ent_tmp[10];

...

//element count of var_tmp
var elements=??get count of elements of "var_tmp"??;


Re: count of array elements [Re: oliver2s] #430810
10/02/13 16:51
10/02/13 16:51
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline
Expert
Kartoffel  Offline
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
afaik no.

but if you really need it you can use a struct to wrap the array and store an integer with the index-count inside that struct.


POTATO-MAN saves the day! - Random
Re: count of array elements [Re: Kartoffel] #430811
10/02/13 16:58
10/02/13 16:58
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
A simple solution for var and int arrays and the like obviously is to save the size in the zero index.
Keep in mind that you lose one index/ have to allocate one more (and I would save "size-1" in the zero index then).


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Re: count of array elements [Re: Kartoffel] #430813
10/02/13 17:09
10/02/13 17:09
Joined: Aug 2002
Posts: 3,258
Mainz
oliver2s Offline OP
Expert
oliver2s  Offline OP
Expert

Joined: Aug 2002
Posts: 3,258
Mainz
Originally Posted By: Kartoffel
afaik no.

but if you really need it you can use a struct to wrap the array and store an integer with the index-count inside that struct.


I already did that, but I have many different structs and each array in each struct have a different size. So I was hoping that there's a general way to find out the element count to save a lot of typing work.


Originally Posted By: Superku
A simple solution for var and int arrays and the like obviously is to save the size in the zero index.
Keep in mind that you lose one index/ have to allocate one more (and I would save "size-1" in the zero index then).


That would be a general solution, but the problem is, most of my arrays are not variable arrays (mostly struct arrays).

Re: count of array elements [Re: oliver2s] #430816
10/02/13 17:47
10/02/13 17:47
Joined: Apr 2005
Posts: 4,506
Germany
F
fogman Offline
Expert
fogman  Offline
Expert
F

Joined: Apr 2005
Posts: 4,506
Germany

Last edited by fogman; 10/02/13 17:56.

no science involved
Re: count of array elements [Re: fogman] #430820
10/02/13 18:18
10/02/13 18:18
Joined: Aug 2002
Posts: 3,258
Mainz
oliver2s Offline OP
Expert
oliver2s  Offline OP
Expert

Joined: Aug 2002
Posts: 3,258
Mainz
Danke für die Links, aber die Beispiele funktionieren nicht (in Lite-C?).

Re: count of array elements [Re: oliver2s] #430822
10/02/13 18:51
10/02/13 18:51
Joined: Oct 2011
Posts: 1,082
Germany
C
Ch40zzC0d3r Offline
Serious User
Ch40zzC0d3r  Offline
Serious User
C

Joined: Oct 2011
Posts: 1,082
Germany
Weil sizeof hier nur 4 (größe eines pointers und var) ausgibt.
Mach einfach nen struct mit void *pPointer für alle pointer und für var etc musst du eigene structs machen.

Re: count of array elements [Re: oliver2s] #430825
10/02/13 19:49
10/02/13 19:49
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
Originally Posted By: oliver2s
Originally Posted By: Kartoffel
afaik no.

but if you really need it you can use a struct to wrap the array and store an integer with the index-count inside that struct.


I already did that, but I have many different structs and each array in each struct have a different size. So I was hoping that there's a general way to find out the element count to save a lot of typing work.


The usual solution for this is to write a generic array container which can automatically updates the storage and does sanity check on passed indices to make sure they stay within the bounds. Let it store void pointers, which are typeless pointers and get automatically promoted to the right type (without explicit cast). If you need scalar types, you have to explicitly cast though, and there is no way around that (besides writing an extra array container for scalar types).

Anyhow, you should really consider doing that, because it has the added benefit that you only have a single point of failure: You don't copy and paste code around, and if there is a bug, you can fix all arrays at once. Besides, doing sanity checks on arrays won't hurt and might allow you to catch out of bugs, if you have anyways.


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: count of array elements [Re: WretchedSid] #430833
10/02/13 21:00
10/02/13 21:00
Joined: Dec 2008
Posts: 1,218
Germany
Rackscha Offline
Serious User
Rackscha  Offline
Serious User

Joined: Dec 2008
Posts: 1,218
Germany
Here is a simple TList "class" i wrote some time ago for my needs of lists:

List.h
Code:
typedef struct TList
{
	int count;
	int* items;
	
}TList;

TList* list_create();
void list_free(TList* AList);

void list_add(TList* AList, void* AItem);
void list_remove(TList* AList, int AIndex);
int list_index_of(TList* AList, void* AItem);
void list_delete(TList* AList, void* AItem);
void list_clear(TList* AList);



List.c
Code:
TList* list_create()
{
	TList* LList = (TList*)sys_malloc(sizeof(TList));
	LList.count = 0;
	LList.items = NULL;
	return(LList);
}

void list_free(TList* AList)
{
	if (AList)
	{
		list_clear(AList);
		sys_free(AList);
	}
}




void list_add(TList* AList, void* AItem)
{
	AList.items = sys_realloc(AList.items, sizeof(int), AList.count, AList.count + 1);
	AList.count += 1;
	(AList.items)[AList.count - 1] = AItem;
}

void list_remove(TList* AList, int AIndex)
{

    int i;
    if(clamp(AIndex, 0, AList.count-1) == AIndex)
    {
        for (i = AIndex; i < AList.count-1; i++)
        {
            (AList.items)[i] = (AList.items)[i+1];
        }
        AList.items = sys_realloc(AList.items, sizeof(int), AList.count, AList.count-1);
        AList.count -= 1;
    }
}

int list_index_of(TList* AList, void* AItem)
{

    int i;
    int LResult = -1;
    for(i = 0; i < AList.count; i++)
    {
        if (AItem == (AList.items)[i])
        {
            LResult = i;
            break;
        }
    }
    return(LResult);
}

void list_delete(TList* AList, void* AItem)
{

    int LIndex = list_index_of(AList,AItem);
    if (LIndex >= 0)
    {
        list_remove(AList,LIndex);
    }
}

void list_clear(TList* AList)
{
	if(AList.items)
	{
		sys_free(AList.items);
		AList.items = NULL;
		AList.count = 0;	
	}
}



and the declaration of sys_realloc:
Code:
int* sys_realloc(void** AIn, int ASize, int AOldCount, int ANewCount)
{

    int i = 0;
    void** LOut;
    LOut = sys_malloc(ASize*ANewCount);
    for(i = 0; i < minv(AOldCount, ANewCount); i++)
    {
        (LOut)[i] = (AIn)[i];
    }
    sys_free(AIn);
    return(LOut);
}



this usually fullfills my needs of lists^^

Greetings
Rackscha


MY Website with news of my projects:
(for example my current
Muliplayer Bomberman,
GenesisPrecompiler for LiteC
and TileMaster, an easy to use Tile editor)
Sparetime-Development

Re: count of array elements [Re: Rackscha] #430846
10/03/13 11:46
10/03/13 11:46
Joined: Aug 2002
Posts: 3,258
Mainz
oliver2s Offline OP
Expert
oliver2s  Offline OP
Expert

Joined: Aug 2002
Posts: 3,258
Mainz
Danke für eure Vorschläge.

Page 1 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1