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?
|
|
0
|
|
|
|
Reply
|
gaya.patel (53)
|
2/5/2005 6:23:47 PM |
|
"G Patel" <gaya.patel@gmail.com> wrote in message
news:1107627827.477295.61180@l41g2000cwc.googlegroups.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
|
|
0
|
|
|
|
Reply
|
me4 (18695)
|
2/5/2005 6:45:54 PM
|
|
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));
/* ... */
}
}
|
|
0
|
|
|
|
Reply
|
mysidia (20)
|
2/5/2005 9:22:46 PM
|
|
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
|
|
0
|
|
|
|
Reply
|
pfiland (6613)
|
2/6/2005 12:18:55 AM
|
|
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.
|
|
0
|
|
|
|
Reply
|
gaya.patel (53)
|
2/6/2005 4:38:23 AM
|
|
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
|
|
0
|
|
|
|
Reply
|
pfiland (6613)
|
2/6/2005 8:01:37 AM
|
|
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
|
|
0
|
|
|
|
Reply
|
pfiland (6613)
|
2/6/2005 8:18:07 AM
|
|
In article <36kij2F4qef1iU1@individual.net>, "Alex Fraser" <me@privacy.net> writes:
> "G Patel" <gaya.patel@gmail.com> wrote in message
> news:1107627827.477295.61180@l41g2000cwc.googlegroups.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 michael.wojcik@microfocus.com
HTML is as readable as C. You can take this either way. -- Charlie Gibbs
|
|
0
|
|
|
|
Reply
|
mwojcik (1874)
|
2/7/2005 5:44:10 PM
|
|
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.
|
|
0
|
|
|
|
Reply
|
k.sengal (3)
|
2/7/2005 8:49:07 PM
|
|
|
8 Replies
32 Views
(page loaded in 0.143 seconds)
|