count of array elements

Posted By: oliver2s

count of array elements - 10/02/13 16:14

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"??;

Posted By: Kartoffel

Re: count of array elements - 10/02/13 16:51

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.
Posted By: Superku

Re: count of array elements - 10/02/13 16:58

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).
Posted By: oliver2s

Re: count of array elements - 10/02/13 17:09

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).
Posted By: fogman

Re: count of array elements - 10/02/13 17:47

http://www.roseindia.net/c-tutorials/c-array-length.shtml

Edit: Unbedingt die zweite Antwort beachten:
http://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c
Posted By: oliver2s

Re: count of array elements - 10/02/13 18:18

Danke für die Links, aber die Beispiele funktionieren nicht (in Lite-C?).
Posted By: Ch40zzC0d3r

Re: count of array elements - 10/02/13 18:51

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.
Posted By: WretchedSid

Re: count of array elements - 10/02/13 19:49

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.
Posted By: Rackscha

Re: count of array elements - 10/02/13 21:00

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
Posted By: oliver2s

Re: count of array elements - 10/03/13 11:46

Danke für eure Vorschläge.
Posted By: sivan

Re: count of array elements - 11/26/13 09:07

@Rackscha: this TList pseudo-class looks great, and thanks for the sys_realloc too!
Posted By: wrekWIRED

Re: count of array elements - 11/26/13 12:31

Can I insert a little noob question rather than making a new thread. Is it possible to use multi dimensional array with ENTITY? tongue
Posted By: sivan

Re: count of array elements - 11/26/13 12:39

probably. create it dynamically by using sys_malloc. just use parantheses carefully afterwards, like (ent_array[i])[j]
© 2024 lite-C Forums