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? 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
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));
/* ... */
}
}
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
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.
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
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
{
|
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)...
|
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;
|
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 {...
|
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...
|
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;
};
...
|
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...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |