By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

Casting Arrays

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a

"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

P: n/a
Thanks - cleared it right up! :)
Jul 22 '05 #3

P: n/a
> 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

P: n/a
"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.