Click here to Skip to main content
Sign Up to vote bad
good
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 2:31am
Edited 12-Sep-12 2:37am


5 solutions

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 - 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 - 12-Sep-12 10:27am
Oh no, the < and > where eaten by HTML :) word16* ptr = reinterpret_cast <word16*> (addr);
Tarun Batra - 12-Sep-12 10:46am
That works for me but i have a problem
Tarun Batra - 12-Sep-12 11:15am
Please look at this http://www.codeproject.com/Questions/457990/Please-is-this-correct-formation
armagedescu - 12-Sep-12 11:13am
What kind of problem?
Tarun Batra - 12-Sep-12 11:22am
please look at this http://www.codeproject.com/Questions/457990/Please-is-this-correct-formation
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 - 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.
  Permalink  
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.
 
char data[6]="CM00";
 
data points to constant.
 
const char data[6]="CM00";
 
Would be correct.
  Permalink  
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:
 

  word16 * waddr = (word16 * ) addr;
  // Main summing loop
  while(count > 1)
  {
    sum = sum + *waddr++;
    count = count - 2;
  }
  Permalink  
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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Christian Graus 453
1 Ron Beyer 316
2 Tadit Dash 243
3 OriginalGriff 236
4 samadhan_kshirsagar 173
0 Sergey Alexandrovich Kryukov 7,061
1 Prasad_Kulkarni 3,830
2 OriginalGriff 3,620
3 _Amy 3,370
4 CPallini 3,074


Advertise | Privacy | Mobile
Web04 | 2.6.130619.1 | Last Updated 12 Sep 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid