Search code examples
c++arraysgccmemset

Can you please Explain the output and point out mistake


char FramebufferUpdateRequest[11];
uint16_t val;
memset(FramebufferUpdateRequest, 0, 10);
FramebufferUpdateRequest[0] = 3;
FramebufferUpdateRequest[1] = 1;
val = 3;
memcpy(FramebufferUpdateRequest+6, &val, 2);
val = 2;
memcpy(FramebufferUpdateRequest+8, &val, 2);
FramebufferUpdateRequest[10]='\0';
printf("framerequest :: %c  %s\n", FramebufferUpdateRequest[1], FramebufferUpdateRequest);

output of this printf is Blank i.e "framerequest :: ".Can anyone point out what I am doing wrong?

compiled in gcc 4.1.2


Solution

  • I think you wanted to write:

    memset(FramebufferUpdateRequest, 0, 10);
    FramebufferUpdateRequest[0] = '3'; //notice the difference
    FramebufferUpdateRequest[1] = '1'; //notice the difference
    val = '3';  //or var = ('3' << 1 | '3') if you want both bytes to have '3'
    memcpy(FramebufferUpdateRequest+6, &val, 2);
    val = '2';  //or var = ('2' << 1 | '2') if you want both bytes to have '2'
    

    Know the difference between '1' and 1:

       cout << (int) ('1') << endl;
       cout << (int) (1) << endl;
    

    Output: ( http://www.ideone.com/z3spn )

    49
    1
    

    Explanation: '1' is a character literal, whose ascii value is 49, whereas 1 is an integer.