473,508 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Access Members of a struct using Array Syntax?

2 New Member
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 5190
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
berrylthird
2 New Member
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
1869
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
2836
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
2178
by: gk245 | last post by:
I have something like this: struct block { int x; int y; float z; }
4
10783
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
367
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
5676
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
2260
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
5911
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
2998
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...
0
7229
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
428
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.