Solved

Accessing members of an array of structs

Posted on 1998-12-17
9
357 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
Comment Utility
like this...
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
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
Comment Utility
RONSLOW
I left an important bit out of the prototype

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

0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:cosmicmike
Comment Utility
RONSLOW

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

Expert Comment

by:RONSLOW
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 while-loops in the C programming language.

772 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

11 Experts available now in Live!

Get 1:1 Help Now