Solved

array of structs problem - HELP

Posted on 1998-12-18
5
289 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 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTML/Webpage Editor for Windows 10 20 142
Intel debugger, idb, only seeing .c and .h, but no .f90 1 297
Problem to ASCII 1 165
Unable to start eclipse ? 17 128
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

914 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now