?
Solved

array of structs problem - HELP

Posted on 1998-12-18
5
Medium Priority
?
352 Views
Last Modified: 2013-12-14
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.
0
Comment
Question by:cosmicmike
  • 3
5 Comments
 

Author Comment

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

0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1255306
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


0
 

Author Comment

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

Ad get this compile error
error C2232: '->i' : left operand has 'struct' type, use '.'
0
 

Author Comment

by:cosmicmike
ID: 1255308
ppUsers is already a double pointer to a struct
0
 
LVL 1

Accepted Solution

by:
lafanga earned 300 total points
ID: 1255309
X_Y *ppUser; // means that *ppUser is a Structure and ppUser is a pointer to the structure

For 1 given structure:
retrieve values by *ppUser.i or ppUser->i

Now, &ppUser is a pointer, to the pointer of the structures
Say &ppUser  = pppUser

So Your first member,  that is a pointer to structure 1 will be pppUser[0] or &ppUser[0]
Now, this pointer (&ppUser[0]) will access data as (&ppUser[0])->i

So the net result should be:
printf("%d\n", (&ppUsers[j])->i);

0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

840 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