cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

expandable arrays, accessing user-defined elements

hanowski
1-Newbie

expandable arrays, accessing user-defined elements

Guru's,
Anybody got a simple example of accessing custom elements from an expandable array?

For example, this correctly prints out what it should:

typedef struct user_appdata {
int TargetCompID;
int TargetDimID;
int ThisCompID;
float NewDimValue;
} UserAppdata;

ProArray DimChanges;

while ( !feof(fIn) ) {
appdata = (UserAppdata*)calloc(1,sizeof(UserAppdata));
fscanf(fIn,"%d,%d,%f\n",&appdata->TargetCompID,&appdata->TargetDimID,&appdata->NewDimValue);
ProArrayObjectAdd((ProArray*)&DimChanges,PRO_VALUE_UNUSED,1,&appdata);
printf("%d,%d,%f\n",appdata->TargetCompID,appdata->TargetDimID,appdata->NewDimValue);
}

But this doesn't:
i = 0;
while ( !feof(fIn) ) {
appdata = (UserAppdata*)calloc(1,sizeof(UserAppdata));
fscanf(fIn,"%d,%d,%f\n",&appdata->TargetCompID,&appdata->TargetDimID,&appdata->NewDimValue);
ProArrayObjectAdd((ProArray*)&DimChanges,PRO_VALUE_UNUSED,1,&appdata);
printf("%d,%d,%f\n",DimChanges[i].TargetCompID,DimChanges[i].TargetDimID,DimChanges[i].NewDimValue);
i++;
}

Thanks,
Greg


This thread is inactive and closed by the PTC Community Management Team. If you would like to provide a reply and re-open this thread, please notify the moderator and reference the thread. You may also use "Start a topic" button to ask a new question. Please be sure to include what version of the PTC product you are using so another community member knowledgeable about your version may be able to assist.
6 REPLIES 6

Greg,

I often use the ProArray*() functions for working with my own data.

You need to use the family of ProArray*() functions. In particular,
ProArrayAlloc() in place of calloc() and ProArrayFree() in place of free().

Bob

--
Robert A. Monat
Jerand Technical Services, Inc.
http://www.jerand.com
Phone: 317-875-6087 FAX: 317-875-6612 Tollfree(US)888-4JERAND


Greg Hanowski wrote:

>Guru's,
>Anybody got a simple example of accessing custom elements from an expandable array?
>
>For example, this correctly prints out what it should:
>
>typedef struct user_appdata {
> int TargetCompID;
> int TargetDimID;
> int ThisCompID;
> float NewDimValue;
>} UserAppdata;
>
>ProArray DimChanges;
>
>while ( !feof(fIn) ) {
>appdata = (UserAppdata*)calloc(1,sizeof(UserAppdata));
>fscanf(fIn,"%d,%d,%f\n",&appdata->TargetCompID,&appdata->TargetDimID,&appdata->NewDimValue);
>ProArrayObjectAdd((ProArray*)&DimChanges,PRO_VALUE_UNUSED,1,&appdata);
>printf("%d,%d,%f\n",appdata->TargetCompID,appdata->TargetDimID,appdata->NewDimValue);
>}
>
>But this doesn't:
>i = 0;
>while ( !feof(fIn) ) {
>appdata = (UserAppdata*)calloc(1,sizeof(UserAppdata));
>fscanf(fIn,"%d,%d,%f\n",&appdata->TargetCompID,&appdata->TargetDimID,&appdata->NewDimValue);
>ProArrayObjectAdd((ProArray*)&DimChanges,PRO_VALUE_UNUSED,1,&appdata);
>printf("%d,%d,%f\n",DimChanges[i].TargetCompID,DimChanges[i].TargetDimID,DimChanges[i].NewDimValue);
>i++;
>}
>
>Thanks,
>Greg
>
>---
>List name: -
>
>You are currently subscribed to toolkits as: bob@jerand.com.
>To unsubscribe send a blank e-mail to -.
>
>For assistance, email us at - or find other contacts at www.ptcuser.org/org/contacts.html.
>
>PTC/USER and the PTC/USER logo are registered trademarks of PTC/USER, Inc.
>Pro/ENGINEER is a registered trademark of PTC.
>
>Message authors are solely responsible for the content of their posts.
>
>
>

I think expandable arrays are now obsolete since the Standard Template
Library became a part of any C++ compiler system. You can use vectors for
your needs very conveniently as well as a lot of other features of C++.


>Guru's,
>Anybody got a simple example of accessing custom elements from an
>expandable array?

I left out a few details in the original email for brevity which has
caused confusion. Here is the entire procedure that outputs the correct
values.


//UserAppdata and DimChanges are globals.
typedef struct user_appdata {
int TargetCompID;
int TargetDimID;
int ThisCompID;
float NewDimValue;
} UserAppdata;

ProArray DimChanges;

ProError LoadDimChanges()
{
UserAppdata *appdata;
int i;

ProCharName name;
FILE *fIn;

ProArrayAlloc(0,sizeof(appdata),1,&DimChanges);
strcpy(name,"deploy.lst");
fIn = fopen(name,"r");
i = 0;
while ( !feof(fIn) ) {
appdata = (UserAppdata*)calloc(1,sizeof(UserAppdata));
fscanf(fIn,"%d,%d,%f\n",&appdata->TargetCompID,&appdata->TargetDimID,&appdata->NewDimValue);

ProArrayObjectAdd((ProArray*)&DimChanges,PRO_VALUE_UNUSED,1,&appdata);
printf("%d,%d,%f\n",appdata->TargetCompID,appdata->TargetDimID,appdata->NewDimValue);

//printf("%d,%d,%f\n",DimChanges[i].TargetCompID,DimChanges[i].TargetDimID,DimChanges[i].NewDimValue);

i++;
}
fclose(fIn);
return(PRO_TK_NO_ERROR);
}

If however, I comment out the printf statement and uncomment the other
one, it prints large, incorrect numbers. I'm apparently not casting
something correctly.

Does anybody have a simple example of accessing custom elements from an
expandable array? If not, I will pursue Vin's suggestion of using
standard C++ vectors. (I'm normally a Delphi programmer, not a C
programmer so please excuse my naivety.)

Greg,

I suggest you check ALL returns from the Pro/E TK functions.

After going through making the changes, I noticed that you had define appdata as a pointer (UserAppdata *appdata) and did not reserve any memory (UserAppdata appdata) for the data. Try this:

ProError LoadDimChanges()
{
UserAppdata appdata;
int i;

ProCharName name;
FILE *fIn;

ProArrayAlloc(0,sizeof(UserAppData),1,&DimChanges);
strcpy(name,"deploy.lst");
fIn = fopen(name,"r");
i = 0;
while ( !feof(fIn) ) {
fscanf(fIn,"%d,%d,%f\n",&appdata.TargetCompID,&appdata.TargetDimID,&appdata.NewDimValue);

ProArrayObjectAdd((ProArray*)&DimChanges,PRO_VALUE_UNUSED,1,&appdata);
printf("%d,%d,%f\n",DimChanges[i].TargetCompID,DimChanges[i].TargetDimID,DimChanges[i].NewDimValue);

i++;
}
fclose(fIn);
return(PRO_TK_NO_ERROR);
}



Greg Hanowski wrote:

>I left out a few details in the original email for brevity which has
>caused confusion. Here is the entire procedure that outputs the correct
>values.
>
>
>//UserAppdata and DimChanges are globals.
>typedef struct user_appdata {
> int TargetCompID;
> int TargetDimID;
> int ThisCompID;
> float NewDimValue;
>} UserAppdata;
>
>ProArray DimChanges;
>
>ProError LoadDimChanges()
>{
> UserAppdata *appdata;
> int i;
>
> ProCharName name;
> FILE *fIn;
>
> ProArrayAlloc(0,sizeof(appdata),1,&DimChanges);
> strcpy(name,"deploy.lst");
> fIn = fopen(name,"r");
> i = 0;
> while ( !feof(fIn) ) {
> appdata = (UserAppdata*)calloc(1,sizeof(UserAppdata));
> fscanf(fIn,"%d,%d,%f\n",&appdata->TargetCompID,&appdata->TargetDimID,&appdata->NewDimValue);
>
> ProArrayObjectAdd((ProArray*)&DimChanges,PRO_VALUE_UNUSED,1,&appdata);
> printf("%d,%d,%f\n",appdata->TargetCompID,appdata->TargetDimID,appdata->NewDimValue);
>
> //printf("%d,%d,%f\n",DimChanges[i].TargetCompID,DimChanges[i].TargetDimID,DimChanges[i].NewDimValue);
>
> i++;
> }
> fclose(fIn);
> return(PRO_TK_NO_ERROR);
>}
>
>If however, I comment out the printf statement and uncomment the other
>one, it prints large, incorrect numbers. I'm apparently not casting
>something correctly.
>
>Does anybody have a simple example of accessing custom elements from an
>expandable array? If not, I will pursue Vin's suggestion of using
>standard C++ vectors. (I'm normally a Delphi programmer, not a C
>programmer so please excuse my naivety.)
>
>---
>List name: -
>
>You are currently subscribed to toolkits as: bob@jerand.com.
>To unsubscribe send a blank e-mail to -.
>
>For assistance, email us at - or find other contacts at www.ptcuser.org/org/contacts.html.
>
>PTC/USER and the PTC/USER logo are registered trademarks of PTC/USER, Inc.
>Pro/ENGINEER is a registered trademark of PTC.
>
>Message authors are solely responsible for the content of their posts.
>
>
>

--
Robert A. Monat
Jerand Technical Services, Inc.
http://www.jerand.com
Phone: 317-875-6087 FAX: 317-875-6612 Tollfree(US)888-4JERAND

We're getting close!

I got a reply from Scott...
"Greg - you need to declare the DimChanges array with some value so that
the C runtime knows the size of its elements to be accessed via []:

UserAppdata** DimChanges;

A ProArray itself should never actually be declared."

That is the key info I was missing. Now it works (almost!). It will
compile ok and print out the first 2 numbers like this...

printf("%d,%d\n",(UserAppdata*)DimChanges[i]->TargetCompID,(UserAppdata*)DimChanges[i]->TargetDimID);



But when I add the 3rd number like this:
printf("%d,%d,%f\n",(UserAppdata*)DimChanges[i]->TargetCompID,(UserAppdata*)DimChanges[i]->TargetDimID,(UserAppdata*)DimChanges[i]->NewDimValue);


The compiler gives this error: "error C2440: 'type cast' : cannot convert
from 'float' to 'UserAppdata *'
There is no context in which this conversion is possible"

It works!

Got the final help again from Scott. "You shouldn't need to do that.
Compiler now knows that DimChanges is a UserAppdata** and so DimChanges[]
is a UserAppdata*.

If you wanted to cast, you'd have to add parentheses to control the order
of operations, currently -> is done before the cast."


So this works:
printf("%d,%d,%f\n",DimChanges[i]->TargetCompID,DimChanges[i]->TargetDimID,DimChanges[i]->NewDimValue);


Thanks everbody (esp Scott) for the help!