?
Solved

Newbie Q: Creating an Array of Pointers

Posted on 2003-03-26
6
Medium Priority
?
244 Views
Last Modified: 2010-04-15
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

0
Comment
Question by:trifusion
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 152 total points
ID: 8213744
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
 
LVL 6

Assisted Solution

by:gj62
gj62 earned 148 total points
ID: 8213900
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
 
LVL 6

Expert Comment

by:gj62
ID: 8213903
Sorry,

change

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

to

strcpy(pdb.fields[0].FieldName, "One");
0
 
LVL 6

Expert Comment

by:gj62
ID: 8214166
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
 
LVL 20

Expert Comment

by:jmcg
ID: 10194615
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question