Solved

array of structs problem - HELP

Posted on 1998-12-18
5
278 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
Comment Utility
ignore this typo:
uReturnVal = lpGetListDet (&iCount, &ppUsers);
should be:
uReturnVal = lpGetList (&iCount, &ppUsers);

0
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
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
Comment Utility
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
Comment Utility
ppUsers is already a double pointer to a struct
0
 
LVL 1

Accepted Solution

by:
lafanga earned 100 total points
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

743 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

16 Experts available now in Live!

Get 1:1 Help Now