468,752 Members | 1,577 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,752 developers. It's quick & easy.

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

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[i] = 0; }

Is this legal?

Nov 14 '05 #1
8 1534
"G Patel" <ga********@gmail.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
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
Nov 14 '05 #2
G Patel wrote:
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[i].a)) : (&(pairs[i].b));
/* ... */
}
}

Nov 14 '05 #3
G Patel wrote:

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[i] = 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.

--
pete
Nov 14 '05 #4

pete wrote:
G Patel wrote:

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[i] = 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.

--
pete


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

Nov 14 '05 #5
G Patel wrote:
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];

--
pete
Nov 14 '05 #6
pete wrote:

G Patel wrote:
> 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?

--
pete
Nov 14 '05 #7

In article <36*************@individual.net>, "Alex Fraser" <me@privacy.net> writes:
"G Patel" <ga********@gmail.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
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.


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.

--
Michael Wojcik mi************@microfocus.com

HTML is as readable as C. You can take this either way. -- Charlie Gibbs
Nov 14 '05 #8
K S
G Patel wrote:
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[i] = 0; }

Is this legal?

No it is not legal.
Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Robert Street | last post: by
1 post views Thread by ravinder thakur | last post: by
8 posts views Thread by Frank Münnich | last post: by
5 posts views Thread by John N. | last post: by
4 posts views Thread by JS | last post: by
14 posts views Thread by Szabolcs Borsanyi | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.