473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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 1653
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert Street | last post by:
Hi! I'm rather new at c++ and I'm totally confused with this kind of typecasting: typedef signed char int8_t; typedef signed short int16_t; typedef struct broadcast_hdr {
5
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
1
by: ravinder thakur | last post by:
hi all experts, i have a structure with the constant memebers such as one given below: typedef struct { const int cbcode; int cberror; } xtsetplatestaterec;
8
by: Frank Münnich | last post by:
Hi there.. My name is Frank Münnich. I've got a question about pointers that refer to an array of a structure. How do I declare that type? If I have declared a structure struct mystruc {...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
7
by: Neil | last post by:
What I am doing wrong This works batPointer = adaptors.adaptor->batData; adaptors.batteries = batPointer->battery; where: batData is a pointer to a struct batPointer is a pointer to a...
14
by: Szabolcs Borsanyi | last post by:
Deal all, The type typedef double ***tmp_tensor3; is meant to represent a three-dimensional array. For some reasons the standard array-of-array-of-array will not work in my case. Can I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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
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,...
0
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...

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.