Link to home
Start Free TrialLog in
Avatar of cosmicmike
cosmicmike

asked on

array of structs problem - HELP

Can somebody tell me how to actually get this to work ??

 I have a DLL that returns a pointer to an array of structs (I think);
 I dont have a Lib file so I have had to load it explicitly (works fine).

 // header snippet
 
 typedef struct
 {
 int i;
 char c[20];
 } X_Y;

 typedef X_Y  *X_Y;

 // DLL prototype
 typedef BOOL (WINAPI *LPGETLIST) (int iCount,  X_Y **ppUsers);
 // Documentation for the DLL function.
 // In args none.
 // Out, returns ..  iCount the number of users.
 // ppUsers ..  a pointer to the users array.
 // The data returned is in the order of the user list, top to bottom
 // The users array is initialized with int i and char c.

 HINSTANCE hDLL;
 LPGETLIST lpGetList;
 X_Y *ppUsers;
 BOOL uReturnVal;
 int iCount, j;
 
 // main snippet

 hDLL = LoadLibrary("The.dll");
 // works fine

 lpGetList = (LPGETLIST) GetProcAddress(hDLL, "TheGetListFunc");
 uReturnVal = lpGetListDet (&iCount, &ppUsers);
 // works fine (uReturnVal TRUE)

// HOW do I access members of the pointed to structs
// I have tried this below DOES NOT WORK

 for(j = 0; j < iCount; j++) {
 printf("%d\n", ppUsers[j].i);      
 // print to screen      
 }

// Free user structures and the array itself.
Avatar of cosmicmike
cosmicmike

ASKER

ignore this typo:
uReturnVal = lpGetListDet (&iCount, &ppUsers);
should be:
uReturnVal = lpGetList (&iCount, &ppUsers);

printf( "%d\n", (*pUsers[j])->i )) ;

The trick is to read hungarian notation (on the front of variable names)

p = pointer to whatever, in your case a struct, therefore use -> to access a member
pp = pointer to pointer
The * dereferences a pointer, so *ppUsers is a pointer to a user struct


Tried this
printf( "%d\n", (*ppUsers[j])->i );  

Ad get this compile error
error C2232: '->i' : left operand has 'struct' type, use '.'
ppUsers is already a double pointer to a struct
ASKER CERTIFIED SOLUTION
Avatar of lafanga
lafanga

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial