Typecasting an Array - TechRepublic
Question
March 1, 2022 at 09:13 AM
Ayush Singh

Typecasting an Array

by Ayush Singh . Updated 4 years, 3 months ago

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.

All Comments