|
-
October 3rd, 2005, 03:43 PM
#6
Re: best way to respresent data
 Originally Posted by Moore
do you know if this is valid?
Yes, it is a correct declaration of structure containing an array.
But, if you want to use it, you will need some work.
For example, here is a usage example:
Code:
#include <stddef.h>
#include <alloc.h>
#include <mem.h>
#include <stdio.h>
typedef enum bool {false=0,true=1} bool;
typedef bool bool_t;
typedef size_t u_int;
struct test {
int val;
char letter;
bool_t flag;
};
typedef struct test test;
typedef struct {
u_int test_List_len;
test *test_List_val;
} test_List;
bool test_List_Construct(test_List *This,u_int size)
{
This->test_List_len=size;
This->test_List_val=(test *)malloc(sizeof(test)*size);
return size==0 || This->test_List_val!=NULL;
}
bool test_List_ConstructFill(test_List *This,u_int size,const test *FillItem)
{
test *p,*pEnd;
if (!test_List_Construct(This,size)) return false;
p=This->test_List_val;
pEnd=p+This->test_List_len;
for(; p!=pEnd ; ++p)
*p=*FillItem;
return true;
}
void test_List_Destroy(test_List *This)
{
free(This->test_List_val);
This->test_List_val=NULL; /* not really needed, but may avoid danling pointers */
This->test_List_len=0;
}
bool test_List_Copy(test_List *NewList,test_List *This)
{
if (!test_List_Construct(NewList,This->test_List_len)) return false;
memcpy(NewList->test_List_val , This->test_List_val , This->test_List_len*sizeof(test));
return true;
}
bool test_List_Resize(test_List *This,u_int new_size)
{
void *NewArray=realloc(This->test_List_val,new_size*sizeof(test));
if (!NewArray) return false;
This->test_List_len=new_size;
This->test_List_val=(test *)NewArray;
return true;
}
bool test_List_Assign(test_List *This,test_List *Source)
{
if (!test_List_Resize(This,Source->test_List_len)) return false;
memcpy(This->test_List_val , Source->test_List_val , Source->test_List_len*sizeof(test));
return true;
}
void TestDisplay(test_List *This)
{
u_int i;
test *pTest;
for(i=0;i<This->test_List_len;++i)
{
pTest=&This->test_List_val[i];
printf("{%i,%c,%s} "
,pTest->val
,pTest->letter
,pTest->flag? "true" : "false" );
}
}
int main()
{
test_List array,array2;
test template={1,'A',false};
u_int i;
test_List_ConstructFill(&array,12,&template);
TestDisplay(&array);printf("\n\n");
for(i=0;i<array.test_List_len;++i)
array.test_List_val[i].val=42-i;
TestDisplay(&array);printf("\n\n");
test_List_Copy(&array2,&array);
TestDisplay(&array2);printf("\n\n");
test_List_Resize(&array2,6);
TestDisplay(&array2);printf("\n\n");
test_List_Assign(&array2,&array);
TestDisplay(&array2);printf("\n\n");
test_List_Destroy(&array2);
test_List_Destroy(&array);
return 0;
}
However, i don't like much the generated names:
It is useless to prefix fields with the structure name, it justs complicate the program if the structure must be renamed, and adds more typing!
I don't like the term list which is abusively used for an array. It should be used for linked-list.
Code:
typedef struct {
u_int len;
test *val;
} test_Array;
This structure is preferable.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|