• 5954阅读
  • 5回复

【提问】這個用法有什麼錯誤嗎??以及它的意思 [复制链接]

上一主题 下一主题
离线btopcst
 
只看楼主 正序阅读 楼主  发表于: 2005-09-24
#include<iostream.h>
typedef unsigned char uint8_t;

void main (void)
{
  void *table_rV[256];
  void *table_gU[256];
  void *table_bU[256];
  for (int i = 0; i < 256; i++) {
  (uint8_t *)table_rV += 1;
  (uint8_t *)table_gU += 1;
  (uint8_t *)table_bU += 1;
  }
}

compiler的訊息::
\Documents and Settings\Administrator\桌面\c++\test_funation\test_funation.cpp(72) : error C2106: '+=' : left operand must be l-value
C:\Documents and Settings\Administrator\桌面\c++\test_funation\test_funation.cpp(73) : error C2106: '+=' : left operand must be l-value
C:\Documents and Settings\Administrator\桌面\c++\test_funation\test_funation.cpp(74) : error C2106: '+=' : left operand must be l-value

問題:這是我看的程式中的一小段用法,我不懂"void *table_rV[256];",

  也不知道要怎麼來改它,麻煩給我個解答…謝謝…
[ 此贴被XChinux在2005-09-24 15:20重新编辑 ]
离线btopcst
只看该作者 5楼 发表于: 2005-10-19
呵呵…我都忘了…我已經知道指標的含義了…謝謝你們…^^
离线XChinux

只看该作者 4楼 发表于: 2005-09-27
其实很简单,你要是把搞明白指针的含义,所有的一切就都迎刃而解了.

第一个例子中,你把地址变了,所以结果跟着变,第二个例子,你是把它的内容变了,而打印出的是地址,所以什么也不变
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线btopcst
只看该作者 3楼 发表于: 2005-09-27
這幾天我也有問一些人…但是都沒什麼結果...所以我做了一個比較…

#include<iostream.h>
void main (void)
{
  unsigned int *table_rV[256];
  for (int i = 0; i < 256; i++) {
  table_rV += i;
  cout<<"table_rV["<<i<<"]="<<table_rV<<endl;
  }
}
執行結果:
table_rV[0]=0xCCCCCCCC
table_rV[1]=0xCCCCCCD0
table_rV[2]=0xCCCCCCD4
table_rV[3]=0xCCCCCCD8
table_rV[4]=0xCCCCCCDC
.
.
table_rV[254]=0xCCCCD0C4
table_rV[255]=0xCCCCD0C8


#include<iostream.h>
void main (void)
{
  void *table_rV[256];
 
  for (int i = 0; i < 256; i++) {
  ((unsigned int*)table_rV) += 1;
cout<<"table_rV["<<i<<"]="<<table_rV<<endl;
 
  }
}
執行結果:
table_rV[0]=0xCCCCCCCD
table_rV[1]=0xCCCCCCCD
table_rV[2]=0xCCCCCCCD
table_rV[3]=0xCCCCCCCD
table_rV[4]=0xCCCCCCCD
.
.
table_rV[254]=0xCCCCCCCD
table_rV[255]=0xCCCCCCCD


結論:第二個CODE…如果寫法有錯…請糾正我的寫法

  至於我為什麼不直接利用第一個CODE來做…因為 void *table_rV[256];

  他可能是…是指向unsigned int…或者是short int…所以才採用第二種寫法…

  麻煩大家~~~~~~~~~~~~~~~~~~~
[ 此贴被btopcst在2005-09-27 23:20重新编辑 ]
离线XChinux

只看该作者 2楼 发表于: 2005-09-24
void *table_rV[256];
void *table_gU[256];
void *table_bU[256];
....................
(uint8_t *)table_rV += 1;
(uint8_t *)table_gU += 1;
(uint8_t *)table_bU += 1;

你是定义了三个指针数组,这样, (uint8_t *)table_rV其实上是一个指针,对指针,只能进行++, --这些操作,不能进行+=这样的操作.
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线XChinux

只看该作者 1楼 发表于: 2005-09-24
+=号的左边必须是l-value(左值)变量.
就是说,你+=左边的变量是不可赋值的.
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
快速回复
限100 字节
 
上一个 下一个