Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

array of structs problem - HELP

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse luna javeEE perspecive missing 5 112
Problem to save 10 174
Problem to scan all sheets 3 111
Eclipse MARs unable to start with exit code=13 2 81
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…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

839 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