can I use a pointer to step through memebers of structures?

G

G Patel

Hi,

As the FAQ and several clc post warn again, I know it's not legal to
use a pointer to step outside the bounds of an array (except one past
the last element, and that pointer must never be dereferenced).

But what about other aggregate objects like structures? If my
structures has arrays, ints, chars, floats, can I just set up a pointer
to a byte (char) and step through the elements of the structure?

A better example would be something like this:

struct {
int a;
int b;
} pairs[30];

int *ptr;
ptr = &pairs[0].a;
/* or ptr = (int *)pairs; */

Now, whenever I want to deal with the a variables separately from the b
variables, I access the structs using normal dot operators, etc.

But there are times when I want to treat the whole thing as 60
conitnuous int variables... can I do this with ptr?

ex:

for(i=0; i < 60; i++) { ptr = 0; }



Is this legal?
 
A

Alex Fraser

G Patel said:
As the FAQ and several clc post warn again, I know it's not legal to
use a pointer to step outside the bounds of an array (except one past
the last element, and that pointer must never be dereferenced).

But what about other aggregate objects like structures? If my
structures has arrays, ints, chars, floats, can I just set up a pointer
to a byte (char) and step through the elements of the structure?

Not usefully (that I can think of), because there may be an arbitrary amount
of padding after members. There usually won't be any padding if the members
are all of one type, but it's not guaranteed.

Alex
 
M

Mysidia

G said:
Hi,
A better example would be something like this:

struct {
int a;
int b;
} pairs[30];

int *ptr;
ptr = &pairs[0].a;
/* or ptr = (int *)pairs; */

Now, whenever I want to deal with the a variables separately from the b
variables, I access the structs using normal dot operators, etc.

But there are times when I want to treat the whole thing as 60
conitnuous int variables... can I do this with ptr?

Try

int i,j, *p;

for (i = 0; i < 30; i++)
{
for(j = 0; j < 2; j++) {
p = (j == 0) ? (&(pairs.a)) : (&(pairs.b));
/* ... */
}
}
 
P

pete

G said:
Hi,

As the FAQ and several clc post warn again, I know it's not legal to
use a pointer to step outside the bounds of an array (except one past
the last element, and that pointer must never be dereferenced).

But what about other aggregate objects like structures? If my
structures has arrays, ints, chars, floats,
can I just set up a pointer
to a byte (char) and step through the elements of the structure?

A better example would be something like this:

struct {
int a;
int b;
} pairs[30];

int *ptr;
ptr = &pairs[0].a;
/* or ptr = (int *)pairs; */

Now, whenever I want to deal with the a
variables separately from the b
variables, I access the structs using normal dot operators, etc.

But there are times when I want to treat the whole thing as 60
conitnuous int variables... can I do this with ptr?

ex:

for(i=0; i < 60; i++) { ptr = 0; }

Is this legal?


Pointer arithmetic is defined in terms of arrays.
Any object can be accessed as an array of unsigned char,
but that's a special case.
The only time you can do math with pointers other than
pointers to char, is when you have either a genuine array
of some non char type, or memory allocated by malloc and friends.
 
G

G Patel

pete said:
G said:
Hi,

As the FAQ and several clc post warn again, I know it's not legal to
use a pointer to step outside the bounds of an array (except one past
the last element, and that pointer must never be dereferenced).

But what about other aggregate objects like structures? If my
structures has arrays, ints, chars, floats,
can I just set up a pointer
to a byte (char) and step through the elements of the structure?

A better example would be something like this:

struct {
int a;
int b;
} pairs[30];

int *ptr;
ptr = &pairs[0].a;
/* or ptr = (int *)pairs; */

Now, whenever I want to deal with the a
variables separately from the b
variables, I access the structs using normal dot operators, etc.

But there are times when I want to treat the whole thing as 60
conitnuous int variables... can I do this with ptr?

ex:

for(i=0; i < 60; i++) { ptr = 0; }

Is this legal?


Pointer arithmetic is defined in terms of arrays.
Any object can be accessed as an array of unsigned char,
but that's a special case.
The only time you can do math with pointers other than
pointers to char, is when you have either a genuine array
of some non char type, or memory allocated by malloc and friends.


An answer with no ambiguities, that's what I wanted. Thanks pete.
 
P

pete

pete said:
G said:
A better example would be something like this:

struct {
int a;
int b;
} pairs[30];

Perhaps a structure with all members the same type
might easily be replaced by an array.

int pairs[30][2];

But then, you should access it properly with two indices.

Maybe you really want an array of 60 int?
 
M

Michael Wojcik

Not usefully (that I can think of), because there may be an arbitrary amount
of padding after members. There usually won't be any padding if the members
are all of one type, but it's not guaranteed.

You can do it "usefully", in the sense of "you can use the structure
members accessed in this manner", by employing an unsigned char
pointer, the offsetof macro, and either memcpy or casting in the
appropriate ways. (Use offsetof to compensate for padding; then
either memcpy to and from a temporary variable, or cast the char
pointer to one of the correct type.)

Whether this is actually useful in any meaningful way is another
question.
 
K

K S

G said:
Hi,

As the FAQ and several clc post warn again, I know it's not legal to
use a pointer to step outside the bounds of an array (except one past
the last element, and that pointer must never be dereferenced).

But what about other aggregate objects like structures? If my
structures has arrays, ints, chars, floats, can I just set up a pointer
to a byte (char) and step through the elements of the structure?

A better example would be something like this:

struct {
int a;
int b;
} pairs[30];

int *ptr;
ptr = &pairs[0].a;
/* or ptr = (int *)pairs; */

Now, whenever I want to deal with the a variables separately from the b
variables, I access the structs using normal dot operators, etc.

But there are times when I want to treat the whole thing as 60
conitnuous int variables... can I do this with ptr?

ex:

for(i=0; i < 60; i++) { ptr = 0; }



Is this legal?

No it is not legal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top