Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot understand C source and it does not compile in GCC, but in Visual C++

Tags:

c

gcc

visual-c++

In GCC I got the following error:

aes.c: In function ‘copy_block’:
aes.c:278: error: lvalue required as increment operand
aes.c:278: error: lvalue required as increment operand

This is the piece of code:

static void copy_block( void * d, void *s, uint_8t nn )
{
    while( nn-- )
        *((uint_8t*)d)++ = *((uint_8t*)s)++;
}

I tried to change it to a compileable version, but unfortunately for me as a Java programmer, it's not clear what is really happening here.

Maybe someone has an idea how I can change the source that it is compileable in GCC or someone has an idea what is happening here in detail. For me it seems wierd with the dereferencing of the left hand value, but somehow it seems to work perfectly in Visual C++.

This is a small legacy program, which I have to port to a Linux machine.

thank you for your help in advance.

like image 893
Exceptiondev Avatar asked Dec 03 '22 09:12

Exceptiondev


2 Answers

Try this:

#include <string.h>
static void copy_block( void * d, void *s, uint_8t nn ) {
  memcpy(d, s, (size_t)nn);
}

What's being done there isn't good.

like image 132
sje397 Avatar answered Feb 23 '23 01:02

sje397


This should do it:

static void copy_block(void *d, void *s, uint_8t nn)
{
    uint_8t *ud = (uint_8t *)d;
    uint_8t *us = (uint_8t *)s;

    while (nn--)
    {
        *ud = *us;
        ++ud;
        ++us;
    }
}

It's also much more readable.

like image 36
detunized Avatar answered Feb 22 '23 23:02

detunized