Link to home
Start Free TrialLog in
Avatar of cosmicmike
cosmicmike

asked on

Accessing members of an array of structs

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 !!
ASKER CERTIFIED SOLUTION
Avatar of RONSLOW
RONSLOW

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RONSLOW
RONSLOW

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 */
  }
}

Avatar of cosmicmike

ASKER

RONSLOW
I left an important bit out of the prototype

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

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 */
  }
}

RONSLOW

I would have thought that to access a member from a pointer
would be pUsers->i ?????????
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.

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.

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.

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