Question

  • Creator
    Topic
  • #3950416

    Typecasting an Array

    by Ayush Singh ·

    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.

    • This topic was modified 2 years, 10 months ago by Avatar photoWizard57M-TR. Reason: Link deactivated to avoid triggering spam filter

You are posting a reply to: Typecasting an Array

The posting of advertisements, profanity, or personal attacks is prohibited. Please refer to our Community FAQs for details. All submitted content is subject to our Terms of Use.

All Answers

  • Author
    Replies
    • #3950554
      Avatar photo

      left me guessing.

      by rproffitt ·

      In reply to Typecasting an Array

      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.

Viewing 0 reply threads