Solved

accessing an array of structures

Posted on 2003-11-29
1
265 Views
Last Modified: 2010-04-15
let's say I declare a data type the following way:
typedef struct DataGroup
{
  int element1;
  char element2[20];
};

Now I would like to declare a type that consists of an array with DataGroup structs. Something like:

typedef struct TestArray
{
  DataGroup element3[10];
};

struct TestArray myTestData;

I have trouble though with the correct syntax. How can I access element1 and element2[] from within myTestData and assign a value to these elements? Please also let me know if there is an easier or better way to do what I describe above. Important is that I have an array of DataGroup.
0
Comment
Question by:MikeJ2001
1 Comment
 
LVL 19

Accepted Solution

by:
Dexstar earned 250 total points
ID: 9844259
@MikeJ2001:

> I have trouble though with the correct syntax. How can I access element1 and
> element2[] from within myTestData and assign a value to these elements? Please
> also let me know if there is an easier or better way to do what I describe
> above. Important is that I have an array of DataGroup.

Like this:
   myTestData.element3[0].element1 = 0;
   myTestData.element3[0].element2[0] = 0;
   myTestData.element3[1].element1 = 0;
   myTestData.element3[1].element2[3] = 0;

Hope That Helps,
Dex*
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

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 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…
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.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

809 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