472,981 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,981 software developers and data experts.

Access Members of a struct using Array Syntax?

This question was inspired by scripting languages such as JavaScript. In JavaScript, I can access members of a class using array syntax as in the following example:

Expand|Select|Wrap|Line Numbers
  1. var myInstance:myClass = new myClass();
  2.  
  3. myInstance.member_0 = memberValue_0; // absolute notation
  4. myInstance[0] = memberValue_0; // relative notation
  5. myInstance["member_0"] = memberValue_0; // nominal notation
You can initialize a struct in C/C++ like you would an array, as with the following code:

Expand|Select|Wrap|Line Numbers
  1. struct myClass {
  2.      int member_0;
  3.      int member_1;
  4.      int member_2;
  5. }
  6.  
  7. myClass myInstance = {memberValue_0, memberValue_1, memberValue_2};
So my question is: Is it possible to access the members of a struct using the array syntax in the same manner as in the following?:

Expand|Select|Wrap|Line Numbers
  1. struct myClass {
  2.      int member_0;
  3.      int member_1;
  4.      int member_2;
  5. } myInstance;
  6.  
  7. myInstance.member_0 = memberValue_0; // proper syntax
  8. myInstance[0] = memberValue_0; // legal?
  9. myInstance["member_0"] = memberValue_0; // legal?
This question is purely academic and serves only my personal curiosity. I haven't worked in C or C++ for over a decade now, and I do not have access to a compiler to verify my question.
Jan 9 '08 #1
2 5146
weaknessforcats
9,208 Expert Mod 8TB
struct myClass {
int member_0;
int member_1;
int member_2;
} myInstance;

myInstance.member_0 = memberValue_0; // proper syntax
myInstance[0] = memberValue_0; // legal?
myInstance["member_0"] = memberValue_0; // legal?
Let's go one at a time:
Expand|Select|Wrap|Line Numbers
  1. myInstance[0] = memberValue_0; // legal?
  2.  
Maybe. Is there a function to ovarload the assignment operator:
Expand|Select|Wrap|Line Numbers
  1. MyClass operator=(Myclass& lhs, MyClass& rhs);
  2.  
???

If there isn't then this is not legal. If there is the compiler will call it to assign the values to myInstance[0].

Then with:
Expand|Select|Wrap|Line Numbers
  1. myInstance["member_0"] = memberValue_0; // legal?
  2.  
As the struct is defined, no.

However, you could write a container struct that contains the array of MyClass and overloads the index operator to look up an object by name.
Jan 9 '08 #2
I was thinking in the case that an array of structs were declared, i.e.:

Expand|Select|Wrap|Line Numbers
  1. myClass myInstance[totalInstances];
  2.  
.....then myInstance[0] could be legal. But even then, to reference myInstance[0] without the dot operator to explicitly call a member, i.e.:

Expand|Select|Wrap|Line Numbers
  1. myInstance[0] = memberValue_0;
.....would it default to the first memory location of the first member of the struct?

Apparently, the answer is NO.

Thank you for your response! I've been coding in scripting languages for so long it would be nice to get back down to some low-level coding for a change.
Jan 9 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: James Brown | last post by:
Hi all, Having problems designing a template-class. I'll describe my scenario first then show what I've come up with so far: Need a class to provide pointer/array-like access to an area of...
5
by: jimjim | last post by:
Hello, Any help will be much appreciatted. My problem is as follows: I declare as global variables: typedef struct _Node{ ..; ..;}Node; Node *Graph; in a function called initiallise(), I...
9
by: gk245 | last post by:
I have something like this: struct block { int x; int y; float z; }
4
by: Erich Pul | last post by:
hi! is it possible to access the members of a struct (e.g. 10 members) in a loop, comparable to an array? tia, E
0
by: Adam Warner | last post by:
Hi all, One cannot return a pointer to an array in C since there are no first class array types. But one can return a pointer to an incomplete array via the illegal but widely supported zero...
9
by: sean.scanlon | last post by:
can someone help understand how i can could access a struct field dymanically like: foo->fields ? when i try to compile this i get the following error: 'struct pwd' has no member named 'fields'...
6
by: zfareed | last post by:
Is it possible to print an array that is declared from a class type that also contains private data members? I have declared an array in the main function and then set the values for the array...
6
by: Urs Thuermann | last post by:
With offsetof() I can get the offset of a member in a struct. AFAICS, it is portable and clean to use this offset to access that member. I need to do something like this struct foo { struct...
6
by: noone | last post by:
What is the syntax to access members of a structure without explicitly naming the structure in every access? struct mytype { int a; char* b; long c; } IT; How can I access the structure...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.