Solved

Accessing members of an array of structs

Posted on 1998-12-17
9
361 Views
Last Modified: 2013-12-14
I am fairly new to C and I am using a fuction in a DLL whose documentation is:
--------------------
typedef struct
{
int i;
} X_Y

// prototype
bool TheFunc(X_Y **ppUsers;
-------------------
// In arguments none.
// Out returns ppUsers a pointer to the Users array.
-------------------
Now when I debug this with variables watch it returns an pointer to an array of structs.

Question how do I access members of individual structs in the array. Eg if there was 5 structs in the array how do get "int i" from the second struct??

Code example please !!
0
Comment
Question by:cosmicmike
  • 7
  • 2
9 Comments
 
LVL 10

Accepted Solution

by:
RONSLOW earned 50 total points
ID: 1255274
like this...
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255275
something like this

X_Y* pUsers;

pUsers = NULL;
if (TheFunc(&pUsers)) {
  /* guess TheFunc returns true if it allocated an array */
  if (pUsers) { /* check anyway */
    int j;
    int n;
    n = 10; /* how do you know how many in the array? */
    for (j = 0; j < n; j++) {
      printf("%d\n",pUsers[j].i);
    }
    /* you may have to free the array afterwards */
  }
}

0
 

Author Comment

by:cosmicmike
ID: 1255276
RONSLOW
I left an important bit out of the prototype

// prototype
bool TheFunc(&iCount, X_Y **ppUsers;

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255277
that would probably change it to..

X_Y* pUsers;
int n;

pUsers = NULL;
n = 0;
if (TheFunc(&pUsers)) {
  /* guess TheFunc returns true if it allocated an array */
  if (pUsers && n != 0) { /* check anyway */
    int j;
    for (j = 0; j < n; j++) {
      printf("%d\n",pUsers[j].i);
    }
    /* you may have to free the array afterwards */
  }
}

0
 

Author Comment

by:cosmicmike
ID: 1255278
RONSLOW

I would have thought that to access a member from a pointer
would be pUsers->i ?????????
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255279
Not when it is an array.

-> is to derefence a pointer to a single item
[j] is to derefence a pointer to one element of an array of items.

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255280
Not when it is an array.

-> is to derefence a pointer to a single item
[j] is to derefence a pointer to one element of an array of items.

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255281
Not when it is an array.

-> is to derefence a pointer to a single item
[j] is to derefence a pointer to one element of an array of items.

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255282
oops .. don't know how that happened. It looks like I'm saying everything three times everything three times everything three times :-)

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Socket Programming (Unix) 8 153
Memory going from 12gb to 64gb or 96gb. worth it? 15 214
Test the speeds on my PC Drives 12 71
windows 10 pro lost profile. 10 71
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…
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 opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

726 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