Question Locked

Typecasting an Array

0 helpful
1 reply
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hello, I just have a question on typecasting an array.
For the code below:

#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
char cbuff[32];
short * sbuff = ( short*) &cbuff;
sbuff[10] = 65;
cout << cbuff[20] ;
cin.get();
}

When I run this on interviewbit.com/online-cpp-compile compiler, the output is 'A'. However, shouldn't the typecasting change the arrays into pairs?
What I thought should happen was: since cbuff[20] and cbuff[21] are paired, when I initialize sbuff[10], the value should go into cbuff[21] first.

1 reply

by rproffitt

For most use the character output is 8 bit ascii. If you need to get into UNICODE there's other issues but you didn't reveal that. Please folk, at least tell what you are trying to do.

This discussion is closed to new replies.