代码我正确为什么我得到'++'需要l值 [英] code i correct why am i getting '++' needs l-value
本文介绍了代码我正确为什么我得到'++'需要l值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我在Visaul Studio中编译此代码时,我得到了以下代码,这是一个a.cpp文件:我收到一个错误:-
错误C2105:'++'在行 sum = sum + *(((word16 *)addr)++ ;;
我该怎么办才能消除错误
#include >< stdio.h > //printf()需要
#include < stdlib.h > //rand()需要
// -----类型定义----------------- -----------------------------------------
typedef unsigned char 字节; // 字节是一个字符
typedef unsigned short // 16位字是一个简短的整数
typedef unsigned int word32; // 32位字是整数
// -----定义------------------ ---------------------------------------------
#define BUFFER_LEN 6//缓冲区长度
// 外部字符数据[6];
字符 data [ 6 ] = " CM00";
// -----原型------------------ ------------------------------------------
word16校验和(字符 * addr,word32计数);
// =====主程序================= ======================================
int tarun( void )
{
// byte buff [BUFFER_LEN]; //数据包字节的缓冲区
word16检查; // 16位校验和值
word32我; // 循环计数器
// 具有BUFFER_LEN随机字节的加载缓冲区
for (i = 0 ; i< BUFFER_LEN; i ++)
{
// buff [i] =(字节)rand();
data [i] =(byte)rand();
}
// 计算16位校验和
// check = checksum(buff,BUFFER_LEN);
校验=校验和(数据,BUFFER_LEN);
// 输出校验和
printf(" ,选中);
}
// ========================= ================================================== ===
// =计算从位置addr开始的计数字节的Internet校验和=
// ========================= ================================================== ===
word16校验和(字符 * addr,word32计数)
{
注册 word32 sum = 0 ;
// 主求和循环
同时(计数> 1 )
{
sum = sum + *(((word16 *)addr)++++;
count = count- 2 ;
}
// 添加剩余字节(如果有)
如果(计数> 0 )
sum = sum + *(((byte *)addr);
// 将32位总和折叠为16位
while (总和>>> 16)
sum =(sum& 0xFFFF)+(sum>> 16 );
返回(〜sum);
}
解决方案
表达式"sum + *((word16 *)addr)"不是左值,即您无法为其分配值.我认为您需要将增量作为单独的语句进行.
这取决于您要增量的值.
如果要增加addr指向的word16,则...
(*((word16 *)addr))++;
...应该这样做.
知道了这一点后,我不确定您要做什么,将addr转换为word16指针看起来有点错误.
再看一下您的代码,我可以说您不应修改addr指向的数据,因为它是恒定数据.
char 数据[ 6 ] = CM00";
数据指向常量.
const char data [ 6 ] = " ;
会是正确的.
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);
}
解决方案 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.
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.
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.
这篇关于代码我正确为什么我得到'++'需要l值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文