Newbie Q: Creating an Array of Pointers

I am trying (with great difficulty) to create an array of pointers to point to data in a record I am reading (from a Palm OS database).

-----------

The layout for a typical record is:

NumFields (2 Byte Integer)
FieldDescr(1) (8 Bytes)
:
FieldDescr(NumFields) (8 Bytes)
FieldData(1) (Length 1)
:
FieldData(N) (Length N)

The data is stored in two parts the first is the description of the fields and the length of the fields. The second part is the Data in each field. I have no problems getting the data into structure for FieldDescr since the structure is static (always 8 bytes). My problem is using the length data from the first part to create an array of pointers to the second part of the data.

so I can create the structure of the FieldDescr fairly easily:
typedef struct FieldDescr {
     Char        FieldName[4];
     UInt16      fieldType;
     UInt16      fieldLength;
     } FieldDescr;
typedef struct pdbLayout {
     UInt16      NumFields;
     FieldDescr; field[];
     } pdbLayout;

But I am having difficulty creating a structure for the FieldData that follows as the second part after the description of the fields in FieldDescr.

Any help would be appreciated.

Thanks

Greg

trifusionAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
imladrisConnect With a Mentor Commented:
Assuming that the record is in memory in some array of bytes, and that you have a pointer to that, and that having filled the FieldDescr structure you know where in the record the data starts you would have two basic variables:

int numfields;
char *record,*p;

record would point to the whole record, and p, now that you have filled the field descriptors, would point to the start of the data in the record. The numfields variables is just the number of fields from the first two bytes in the record. I'm guessing that the difficulty may be connected to the fact that the data pointer array needs to be dynamic in length. So, now you should be able to create the data pointer array along the following lines:

char **data;

data=malloc(numfields*sizeof(char *));
for(i=0; i<numfields; ++i)
{   data[i]=p;
    p+=pdb.field[i].fieldLength;
}

data needs to be an array, but you don't know how long it needs to be. So data points to an array of pointers (i.e. it is a pointer to a pointer). Then you malloc the space for the required number of pointers, then you initialize each pointer to point to the right spot in the record.
0
 
gj62Connect With a Mentor Commented:
If I read you correctly, you need to create an array of FieldDescr within a single pdbLayout struct.  You don't know the number of FieldDescr you will need at compile-time, but at run-time you will get it in pdbLayout.NumFields.  If this is correct, then the following should help...

typedef struct FieldDescr {
    Char        FieldName[4];
    UInt16      fieldType;
    UInt16      fieldLength;
    } FieldDescr;

typedef struct pdbLayout {
    UInt16      NumFields;
    FieldDescr  *fields;   /* will be your array */
    } pdbLayout;

pdbLayout pdb;

/* do something to get the number of fields...*/
pdb.NumFields = ...;

/* now, create a pointer to an array of FieldDesc...*/
pdb.fields = (FieldDescr *)malloc(NumFields *sizeof(FieldDescr));

/*You now have an array of FieldDescr structures, pointed to by pdb.fields.  */

strcpy(pdb.fields[0]->FieldName, "One");

 will save the string One in the FieldName member of the first FieldDescr struct in the array...

Now, that deals with your field descriptions, but you haven't anywhere to save the actual data in those fields - do you need that too???
0
 
gj62Commented:
Sorry,

change

strcpy(pdb.fields[0]->FieldName, "One");

to

strcpy(pdb.fields[0].FieldName, "One");
0
 
gj62Commented:
If you need to now store the data, can you make a pass of the data to know how many records there are, and then make a second pass to store them?  If so, we can dynamically allocate a multi-dimensional array.  If not, we'll have to preallocate a certain number of records, and then come up with a Create/Copy/Free mechanism to allocate more storage, copy the data and free the old storage when we need more space.
0
 
jmcgOwnerCommented:
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
split points between impladris and gj62.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0
All Courses

From novice to tech pro — start learning today.