Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C Visual-Studio VC++
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
 
#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 12-Sep-12 3:31am
Edited 12-Sep-12 3:37am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Tarun Batra at 12-Sep-12 8:47am
   
can u tell how to increment as a seperate statement.
@amitgajjar at 12-Sep-12 8:59am
   
store that value in other variable and then use increment operator.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Tarun Batra at 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 at 12-Sep-12 9:34am
   
But the extern code is commented. Is it declared somewhere else?
What error you get at 'extern' ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
 
char data[6]="CM00";
 
data points to constant.
 
const char data[6]="CM00";
 
Would be correct.
  Permalink  
Comments
Legor at 12-Sep-12 10:18am
   
You should have updated your first solution instead of writing another one using the "Improve solution" button.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Why don't you write it this way:
 

  word16 * waddr = (word16 * ) addr;
  // Main summing loop
  while(count > 1)
  {
    sum = sum + *waddr++;
    count = count - 2;
  }
  Permalink  
Comments
Tarun Batra at 12-Sep-12 10:27am
   
same error
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

The error is because of you want to increase a char pointer by operation for short int operator. The pointer values are compatible, but pointer operations not.
Try to create other pointer and assign the address.
 
word16 ptr = reinterpret_cast (addr);
 
After that, there should be no problem:
sum = sum + *ptr++;
  Permalink  
Comments
Tarun Batra at 12-Sep-12 10:27am
   
i did word16 * waddr = reinterpret_cast(addr); it says expected a "<" i did this word16 * waddr = reinterpret_cast; it says expected "(".
armagedescu at 12-Sep-12 10:27am
   
Oh no, the < and > where eaten by HTML :)
word16* ptr = reinterpret_cast <word16*> (addr);
Tarun Batra at 12-Sep-12 10:46am
   
That works for me but i have a problem
Tarun Batra at 12-Sep-12 11:15am
   
Please look at this http://www.codeproject.com/Questions/457990/Please-is-this-correct-formation
armagedescu at 12-Sep-12 11:13am
   
What kind of problem?
Tarun Batra at 12-Sep-12 11:22am
   
please look at this http://www.codeproject.com/Questions/457990/Please-is-this-correct-formation

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 120
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,611
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 12 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100