struct member access with array notation?

I could have sworn I've seen array notation used in the past for accessing struct member values. Maybe, it was another language and I'm just mixing syntax up. But, I thought it was possible to access struct values like so.

1
2
3
4
5
6
7
8
9
10
11
12
typedef struct
{
  int firstValue;
  int secondValue; 
} Foo;

main ()
{
  Foo foo;
  foo[0] = 1;
  foo[1] = 2;
}


Is there actually way to do syntax like that depending on how the struct is defined? Does it have to be a pointer for that type of access?
Last edited on
Are you asking about C?

Well, anyway.
It is not possible.
You could do some fancy arithmetic with pointers but it is completely not recommended.

A better solution is it put those members into an array for that form of access:
1
2
3
4
5
6
7
8
9
10
typedef struct{
  int values[2];
} Foo;

main ()
{
  Foo foo;
  foo.values[0] = 1;
  foo.values[1] = 2;
}


If you are using C++, you can overload the [] operator to return references to these values. But an issue would arise if the members are of different types.
Last edited on
yes, the pointer access works but its dangerous.
I don't remember what the standard ensures and what just works on most compilers...

you would do it like this..

struct badidea
{
int a,b,c,d;
};

badidea bi;
int * ip = &bi.a;
ip[0]; //a
ip[1]; //b
...

this is just horrible. But you may as well see it, because people do junk like this from time to time, esp in older code.


a better approach:

enum {val1, example, val3, val4, maxval};
int vals[maxval];

vals[example]; //access the array elements via a name.

you can wrap that into a class or whatever. you can do it with a vector also, so long as you know how many items you have up front. Note that using the maxval trick, if you insert some new ones, the code still works, as that grows the array upon recompile... fixes all the loops (based off maxval) etc... its all good.


Last edited on
I'm just doing this for a range based container's iterator, so I can modify contents of pointers stored in the struct during the loop, and have the iterator updated with changes. I was just hoping to use array notation since it would be a little more concise.

If to access the members like that involves pointers, I won't be doing that. I could only see a good reason for writing code like that if it's directly interacting with memory in a more complicated way than I currently am.

Currently, I'm having the iterator return a char *, but I want some functions for inserting into my container to update the position of the container after inserting, so I don't iterate over the inserted element.
there really isn't ever a good reason for doing that. And some systems align struct members funky so it wouldn't even work, but in practice, for ints at least, it usually does work.

Not sure what you are asking for your data structure.
I think there's just examples of it in my system architecture book, for working with structs that have union members.

Since I'm working with char * that notation wouldn't really make sense.
Topic archived. No new replies allowed.