Only this week 80 % discount on e-learning courses of C# .NET
Get up to 60 % extra points for free! More info
discount 50

Discussion: Typecasting an Array

Kiran Kumar
Kiran Kumar:17. January 7:47

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] ;

When I run on this online cpp 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.

Can anyone explain to me what is going on?

17. January 7:47
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

1 messages from 1 displayed.