访问指针指向的值

时间:2013-05-03 10:09:38

标签: c++ pointers structure

编辑:这个问题应该被视为放弃了。我已经将这个问题标记为删除,因为我不知道如何在这一点上继续进行。我感谢你们所有人的意愿,并抽出时间来帮助我。

我正在阅读并遵循cplusplus关于数据结构的文档。我一直试图弄清楚为什么编译器不会在我自己接受“* pfruit.weight;”几个小时。我相信这很简单,我不知道。

error C2228: left of '.weight' must have class/struct/union
1>          type is 'product *'
1>          did you intend to use '->' instead?

“句点(。)左边的操作数不是类,结构或联合。”

那么我如何正确访问指针成员指向的值?(不访问引用)

#include <iostream>

using namespace std;

struct product
{
int weight;
float price;
} ;

int main ()
{
product afruit;
product * pfruit;
pfruit = &afruit;

pfruit->weight;
    //Access a member of an object to which we have a reference
//equivalent to: (*pfruit).weight

*pfruit.weight; //<------ I am so sorry, i meant This is the problem, i am using a reworded line of code from an example and it isn't compiling and returning the error.
    //Access the value pointed by a pointer member called weight;
//equivalent to: *(pfruit.weight)

system("pause");
return 0;
}

5 个答案:

答案 0 :(得分:4)

此代码

struct product
{
int weight;
float price;
} ;

int main ()
{
    product * pfruit;

    *pfruit.weight;

运算符优先级错误。 C ++的规则是.的优先级高于*。如果*pfruit.weight是结构而pfruit是指针,则weight会正确,但在代码中则相反,pfruit是指针weight只是一个int。因此,您需要添加括号以正确应用运算符

    (*pfruit).weight;

答案 1 :(得分:1)

您可以使用以下声明:

(*pfruit).weight;

而不是:

*pfruit.weight;

由于运算符优先级而在编译时评估以下内容:

*(pfruit.weight)

这是错误的,因为您正在取消引用非指针值并尝试 使用.运算符而不是->取消引用指针。

编辑:

根据我的理解,这里有关于指针和问题的一些回应和信息。

#include <iostream>

using namespace std;

struct product
{
  int   *weight;
  float price;
};

int             main()
{
  product       afruit;
  product       *pfruit;

  pfruit = &afruit;

  // Allocating 10 * sizeof(int) bytes of memory. ptr will now point to the first sizeof(int) bytes                                                                                                                                          
  // of the memory pointed by the weight pointer.                                                                                                                                                                                            
  pfruit->weight = new int[10];

  // The first sizeof(int) bytes of the memory pointed by ptr (equivalent to *ptr) equals now the value 4.                                                                                                                                   
  pfruit->weight[0] = 4;

  // The second sizeof(int) bytes of memory pointed by ptr (or (*ptr + 1)) equals now 42.                                                                                                                                                    
  pfruit->weight[1] = 42;

  // Dereferencing pfruit AND the pointer to the weight, thus returning the VALUE of the first sizeof(int) bytes of weight (4)                                                                                                               
  // instead of the value of the pointer to weight (0x8b4e008 for instance).                                                                                                                                                                
  int value = *(pfruit->weight);

  std::cout << "Value of pfruit->weight[0] = " << pfruit->weight[0] << std::endl
            << "Value of pfruit->weight[1] = " << pfruit->weight[1] << std::endl
            << "Value of *(pfruit->weight) = " << *(pfruit->weight) << std::endl
            << "Value of *(pfruit->weight + 1) = " << *(pfruit->weight + 1) << std::endl
            << "Value of ptr = " << std::hex << pfruit->weight << std::endl;

  // Prints :                                                                                                                                                                                                                                
  // Value of pfruit->weight[0] = 4                                                                                                                                                                                                          
  // Value of pfruit->weight[1] = 42                                                                                                                                                                                                         
  // Value of *(pfruit->weight) = 4                                                                                                                                                                                                          
  // Value of *(pfruit->weight + 1) = 42                                                                                                                                                                                                     
  // Value of ptr = 0x8b4e008                                                                                                                                                                                                                
  //                                                                                                                                                                                                                                         
  // Note that ptr[0] == *ptr and ptr[1] == (*ptr + 1)                                                                                                                                                                                       
  return (0);
}

答案 2 :(得分:0)

enter image description here

一旦尝试,你可以得到它,这可以写成pfruit-&gt;重量或(* pfruit).weight

   (*pfruit).weight;

答案 3 :(得分:0)

product afruit;
product * pfruit;
pfruit = &afruit;

pfruit->weight; 

pfruit是一个指针,具有结构afruit的地址。 当我们使用指针访问struct成员时,我们使用pfruit->weight

使用结构名称时,使用.运算符。 现在,pfruit是指针。因此,结构本身是*pfruit

*pfruit.weight;

这被解释为*(pfruit.weight),因为.的{​​{3}}高于*。并且你不能使用.运算符和LHS上的指针

您必须清楚地表明您的意思是(*pfruit).weight

答案 4 :(得分:0)

派对迟到了,但试试这个:

听起来你正在期待像“重量”这样的价值观。以与通过pfruit引用产品相同的方式存储为其他地方的数据指针。有些语言会这样做,但在C ++中,像您的体重和价格这样的简单数据成员将实际存储在产品数据结构中。因此,如果在内存中的地址1000处存在fruit,则pfruit将具有值1000:

... 1000  1001  1002  1003  ...
  --+-----+-----+-----+-----+--
    | afruit... |     |     |
  --+-----+-----+-----+-----+--
      ^
      |
      pfruit = 1000

这意味着您的weight(int)和price(float)数据成员将一个接一个地存储在此处。 (由于int和float的大小通常为32位,即4个字节,因此内存如下所示:

... 1000  1001  1002  1003  1004  1005  1006  1007  ...
  --+-----+-----+-----+-----+-----+-----+-----+-----+--
    | afruit...                                     |
    | <------ weight -----> | <------ price ------> |
  --+-----+-----+-----+-----+-----+-----+-----+-----+--
      ^
      |
      pfruit = 1000

因此,pfruit也恰好指向权重数据(因为它是结构中的第一个),但价格数据还是4个字节。

所以,当你说你想要获得权重指出的数据时,&#39;它没有意义,因为它实际上证明你不需要,因为当你访问权重成员时,你已经指的是权重值。它就在那里,在水果所占据的记忆中。

如果你试图获得一个指向权重(或成本)数据本身的指针(而不是afruit,或者实际上来自pfruit)你可以做这样的事情:

int* pweight = &afruit.weight;  //from an actual product object
float* pcost = &pfruit->cost;   //via a pointer to a product object

现在有各种方式可以访问产品结构的成员:

afruit.weight = 20;     //directly access the structure member
pfruit->weight = 10;    //follow the structure pointer
*pweight = 15;      //derefernce the int pointer

我希望这有助于某人,至少一点点。

萨姆