468,458 Members | 1,823 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Casting Arrays

Howdy,

Put briefly, I have an array of chars, which I would like to access in pairs
of bytes via casting the array to an array of shorts. I'm trying to be as
elegant as possible.

Below is a program with a failed attempt. Hopefully it will help clarify the
issue :).

Thanks!
David

#include <iostream>
int main() {
char cbuff[32];
short * sbuff = (* short) &cbuff; // my failed attempt
sbuff[10] = 65;
cout << cbuff[21] ; // should output A
}
Jul 22 '05 #1
4 17511

"David Rager" <ra*****@nospammemail.utexas.edu> wrote in message
news:bp**********@geraldo.cc.utexas.edu...
Howdy,

Put briefly, I have an array of chars, which I would like to access in pairs of bytes via casting the array to an array of shorts. I'm trying to be as
elegant as possible.

Below is a program with a failed attempt. Hopefully it will help clarify the issue :).

Thanks!
David

#include <iostream>
int main() {
char cbuff[32];
short * sbuff = (* short) &cbuff; // my failed attempt
sbuff[10] = 65;
cout << cbuff[21] ; // should output A
}


In your cast, you need (short *) rather than (* short).

You'll also need std::cout instead of cout since you didn't "using namespacr
std;"...
Jul 22 '05 #2
Thanks - cleared it right up! :)
Jul 22 '05 #3
> Put briefly, I have an array of chars, which I would like to access in
pairs
of bytes via casting the array to an array of shorts. I'm trying to be as
elegant as possible.


I must confess I have trouble understanding a context in which this kind of
type cheating can be considered elegant.

What problem are you trying to solve?
Jul 22 '05 #4
"David Rager" <ra*****@nospammemail.utexas.edu> wrote in message news:<bp**********@geraldo.cc.utexas.edu>...
Howdy,

Put briefly, I have an array of chars, which I would like to access in pairs
of bytes via casting the array to an array of shorts. I'm trying to be as
elegant as possible.


Hi David,

Let me preface by saying that I'm not all that experienced with c++,
and so there may be other issues with what I'm about to write.

I had the need to do similar casting recently...I was accessing a char
array as an array of unsigned long ints. Someone raised the issue of
"alignment", which could present portability problems. The issue is
basically this. Suppose you have an array of 10 ints, a[10], and you
want to access the 2nd & 3rd bytes as a short (ie a[1] & a[2]). It is
possible that short can only be aligned with bytes that are an
even-multiple offset from the start of the memory space. That is,
your short int pointer can only point to(in char*) a[0], a[2], a[4],
a[6], a[8] indexed as ashort[0] - ashort[4].

What I ended up doing was to turn the problem around. I made a class,
AlignInt, that creates an empty int (or short) array, loads the
char-data into that array, and then give the user pointers to that
data as signed or unsigned char, short, int, and long. So...for
example, if I had the char string "abcdefgh" and I wanted to access
short int words from the same memory location as "bc", "de", "fg",
"hNULL", (note this case doesn't use the first byte...this is the sort
of case that could cause alignment issues). I first create an empty
4-word array of short, cast the array to char*, load the substring
"bcdefgh" into the array, then safely access as either data-type.

Anyway...just food for thought. If you want to see or use this class,
just ask. It accepts string, a file, or an existing aligned array,
and gives public access to the data and array length represented as
any of the native data-types.
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by ghostdog | last post: by
4 posts views Thread by Stewart Stevens | last post: by
4 posts views Thread by John Ratliff | last post: by
61 posts views Thread by Ken Allen | last post: by
5 posts views Thread by Diffident | last post: by
2 posts views Thread by stunt016 | last post: by
13 posts views Thread by Boon | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.