Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
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

C++
#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);
}
Posted
Updated 12-Sep-12 2:37am
v2

the expression "sum + *((word16 *) addr)" is not an lvalue i.e. you cannot assign to it. I think you will need to do the increment as a seperate statement.
 
Share this answer
 
Comments
Tarun Batra 12-Sep-12 8:47am    
can u tell how to increment as a seperate statement.
AmitGajjar 12-Sep-12 8:59am    
store that value in other variable and then use increment operator.
That will depend on what value you want to increment.
If you want to increment the word16 that addr is pointing to, then ...

(*((word16 *) addr))++;

... should do it.

Having sead that, I am not sure what you are trying to do, casting addr to a word16 pointer looks a bit wrong.
 
Share this answer
 
Comments
Tarun Batra 12-Sep-12 9:08am    
sir if i compile the file as a.c then i am not not getting this error,but i am getting the error on "extern" variable.Does extern variable work in c ?
Malli_S 12-Sep-12 9:34am    
But the extern code is commented. Is it declared somewhere else?
What error you get at 'extern' ?
Having looked at your code a bit more I can say that you should not modify the data that addr is pointing to because it is constant data.

C++
char data[6]="CM00";


data points to constant.

C++
const char data[6]="CM00";


Would be correct.
 
Share this answer
 
Comments
Legor 12-Sep-12 10:18am    
You should have updated your first solution instead of writing another one using the "Improve solution" button.
Why don't you write it this way:
C++
word16 * waddr = (word16 * ) addr;
// Main summing loop
while(count > 1)
{
  sum = sum + *waddr++;
  count = count - 2;
}
 
Share this answer
 
Comments
Tarun Batra 12-Sep-12 10:27am    
same error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900