C pointers question

Hi,
I'm not sure what I'm doing wrong in my code. My purpose is to have a buffer (char buf[]) and modify some of its bytes. I have this code:

void sett(char *bufy){
unsigned short sh=*((unsigned short *)&bufy);
printf("initially short %d
",sh);
memcpy(bufy,(char *)&sh,2);
sh=*((unsigned short *)&bufy);
printf("after short, should be same %d
",sh);
}

In the code, I just want to plug out a short from the buffer and rewrite back the

same 2 bytes. But somehow I don't get the same result as initially started with.
I would greatly appreciate any help,
Thank you.
F.

Comments

  • : [code]
    : void sett(char *bufy){
    : unsigned short sh=*((unsigned short *)&bufy);
    : printf("initially short %d
    ",sh);
    : memcpy(bufy,(char *)&sh,2);
    : sh=*((unsigned short *)&bufy);
    : printf("after short, should be same %d
    ",sh);
    : }
    : [/code]
    : In the code, I just want to plug out a short from the buffer and rewrite back the
    :
    : same 2 bytes. But somehow I don't get the same result as initially started with.
    : I would greatly appreciate any help,
    : Thank you.
    : F.
    :
    [blue]That is because CPU stores values in backward order. Store the SHORT value into string and look into its contents. You will see that bytes in a short value are reversed. When you move memory, however, the order of bytes is not changed, of course. If you want to move the short value back into buffer, you have to use the same method you used when taking it out:
    [code]
    // CODE 1:

    char s [20];
    unsigned short value1 = 6527;
    unsigned short value2;

    memcpy (s, &value1, 2);
    memcpy (&value2, s, 2);

    // CODE 2:

    char s [20];
    unsigned short value1 = 6527;
    unsigned short value2;

    ((unsigned short*) s) [0] = value1;
    value2 = ((unsigned short*) s) [0];
    [/code]

    Both of these code samples will produce the same result: short value will be moved from one location to the other, however, I fail to understand why you can't just use operator '=':

    [code]
    value2 = value1;
    [/code]
    [/blue]
  • : : [code]
    : : void sett(char *bufy){
    : : unsigned short sh=*((unsigned short *)&bufy);
    : : printf("initially short %d
    ",sh);
    : : memcpy(bufy,(char *)&sh,2);
    : : sh=*((unsigned short *)&bufy);
    : : printf("after short, should be same %d
    ",sh);
    : : }
    : : [/code]
    : : In the code, I just want to plug out a short from the buffer and rewrite back the
    : :
    : : same 2 bytes. But somehow I don't get the same result as initially started with.
    : : I would greatly appreciate any help,
    : : Thank you.
    : : F.
    : :
    : [blue]That is because CPU stores values in backward order. Store the SHORT value into string and look into its contents. You will see that bytes in a short value are reversed. When you move memory, however, the order of bytes is not changed, of course. If you want to move the short value back into buffer, you have to use the same method you used when taking it out:
    : [code]
    : // CODE 1:
    :
    : char s [20];
    : unsigned short value1 = 6527;
    : unsigned short value2;
    :
    : memcpy (s, &value1, 2);
    : memcpy (&value2, s, 2);
    :
    : // CODE 2:
    :
    : char s [20];
    : unsigned short value1 = 6527;
    : unsigned short value2;
    :
    : ((unsigned short*) s) [0] = value1;
    : value2 = ((unsigned short*) s) [0];
    : [/code]
    :
    : Both of these code samples will produce the same result: short value will be moved from one location to the other, however, I fail to understand why you can't just use operator '=':
    :
    : [code]
    : value2 = value1;
    : [/code]
    : [/blue]
    :

    I haven't tried the code, but to me it seems that the error lies somewhere else.
    During both copy actions, the bytes of the short are not swapped, are they? So the results should be the same.
    The reason for getting an incorrect value lies in the following line:
    [code]
    sh=*((unsigned short *)&bufy);
    [/code]
    bufy is a pointer already, so using the & in front of it gives you the address of the variable, which is probably somewhere on the stack if it is a local one, while you need contents of bufy.
    So modify the line as follows, and you'll be fine.
    [code]
    sh=*((unsigned short *)bufy);
    [/code]


    Greets,
    Eric Goldstein
    http://www.gvh-maatwerk.nl/english/E_index.htm


  • This should work.

    [code]
    void sett(char *bufy)
    {
    unsigned short sh=*((unsigned short *)bufy);
    printf("initially short %d
    ",sh);
    memcpy(bufy,&sh,2);
    sh=*((unsigned short *)bufy);
    printf("after short, should be same %d
    ",sh);
    }
    [/code]

    The result will depend on whether the CPU is using little or big endian.
  • : This should work.
    :
    : [code]
    : void sett(char *bufy)
    : {
    : unsigned short sh=*((unsigned short *)bufy);
    : printf("initially short %d
    ",sh);
    : memcpy(bufy,&sh,2);
    : sh=*((unsigned short *)bufy);
    : printf("after short, should be same %d
    ",sh);
    : }
    : [/code]
    :
    : The result will depend on whether the CPU is using little or big endian.
    :
    Isn't that exactly what I said?

    And the topic here was to have sh contain the same value before and after the copy. That does *not* depend on the CPU.


    Greets,
    Eric Goldstein
    http://www.gvh-maatwerk.nl/english/E_index.htm


  • : : This should work.
    : :
    : : [code]
    : : void sett(char *bufy)
    : : {
    : : unsigned short sh=*((unsigned short *)bufy);
    : : printf("initially short %d
    ",sh);
    : : memcpy(bufy,&sh,2);
    : : sh=*((unsigned short *)bufy);
    : : printf("after short, should be same %d
    ",sh);
    : : }
    : : [/code]
    : :
    : : The result will depend on whether the CPU is using little or big endian.
    : :
    : Isn't that exactly what I said?


    [blue]Almost. I removed some typecasts etc.[/blue]


    :
    : And the topic here was to have sh contain the same value before and after the copy. That does *not* depend on the CPU.
    :

    [blue]Yes, it will be the same value. But which symbols in the string that will be transfered to the integer is CPU dependant.
    [/blue]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories