What should i do to remove the error '++' needs l-value
Tarun Batra (72)
Sep 12, 2012 at 12:40pm UTC
I have the following code its an a.cpp file when i compile this code in visaul studio i am getting an error :-
error C2105: '++' needs l-value
at the line
sum = sum + *((word16 *) addr)++;
What should i do to remove the error
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include <stdio.h> // Needed for printf()
#include <stdlib.h> // Needed for rand()
//----- Type defines ----------------------------------------------------------
typedef unsigned char byte; // Byte is a char
typedef unsigned short int word16; // 16-bit word is a short int
typedef unsigned int word32; // 32-bit word is an int
//----- Defines ---------------------------------------------------------------
#define BUFFER_LEN 6 // Length of buffer
//extern char data[6];
char data[6]="CM00" ;
//----- Prototypes ------------------------------------------------------------
word16 checksum(char *addr, word32 count);
//===== Main program ==========================================================
int tarun(void )
{
//byte buff[BUFFER_LEN]; // Buffer of packet bytes
word16 check; // 16-bit checksum value
word32 i; // Loop counter
// Load buffer with BUFFER_LEN random bytes
for (i=0; i<BUFFER_LEN; i++)
{
//buff[i] = (byte) rand();
data[i]=(byte) rand();
}
// Compute the 16-bit checksum
//check = checksum(buff, BUFFER_LEN);
check = checksum(data, BUFFER_LEN);
// Output the checksum
printf("checksum = %04X \n" , check);
}
//=============================================================================
//= Compute Internet Checksum for count bytes beginning at location addr =
//=============================================================================
word16 checksum(char *addr, word32 count)
{
register word32 sum = 0;
// Main summing loop
while (count > 1)
{
sum = sum + *((word16 *) addr)++;
count = count - 2;
}
// Add left-over byte, if any
if (count > 0)
sum = sum + *((byte *) addr);
// Fold 32-bit sum to 16 bits
while (sum>>16)
sum = (sum & 0xFFFF) + (sum >> 16);
return (~sum);
}
TheIdeasMan (1564)
Sep 12, 2012 at 12:51pm UTC
Looks to me as though you are trying to cast the addr value into a word16 value. Not sure you can do that via a typedef.
I use stdint.h which defines types like uint16_t which is is an unsigned 16 bit quantity. There are 8, 16, 32, 64 bit values and unsigned for each one, plus 'fast' versions which use the machine word size.
You might be able to then use a dynamic_cast to achieve you casting goal.
sorry I see it is a C program ( although you have it in a .cpp file)
Also
can be shortened to
It works for other operators too.
Tarun Batra (72)
Sep 12, 2012 at 12:56pm UTC
Same error,sir one thing more can u tell does "extern some variable " doessn't work in C,i mean does "extern" variable works in c?
TheIdeasMan (1564)
Sep 12, 2012 at 1:18pm UTC
word16 checksum(char *addr, word32 count)
*((word16 *) addr)++
OK look at what this means.
It looks like you want to cast a pointer to an 8 bit quantity to a pointer to a 16 bit quantity. Try dereferencing them first, then use unsigned short to cast.
Edit: try dereferencing them then assignment.
extern variable means the variable is declared in another file. Google it
Last edited on Sep 12, 2012 at 1:19pm UTC
Topic archived. No new replies allowed.