简体   繁体   中英

Accessing the value pointed by a pointer

Edit: This question should be considered abandoned. I have flagged this question for deletion as i do not know how to even proceed any more at this point. I thank all of you for your willingness and taking the time out of your day to help me.

I am reading and following the documentation at cplusplus on data structures. I have been trying to figure out why the compiler will not accept " *pfruit.weight; " on my own for hours. It am sure it is something simple that i am missing.

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

"The operand to the left of the period (.) is not a class, structure, or union."

So how do i correctly access the value pointed by a pointer member?(Not access a reference)

#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;
}

This code

struct product
{
int weight;
float price;
} ;

int main ()
{
    product * pfruit;

    *pfruit.weight;

is an operator precedence error. The rules of C++ are that . has higher precedence than * . *pfruit.weight would be correct if pfruit was a struct and weight was a pointer, but in your code it's the other way around, pfruit is the pointer and weight is just an int. So you need to add brackets to apply the operators the right way around

    (*pfruit).weight;

You may use the following statement :

(*pfruit).weight;

Instead of :

*pfruit.weight;

which evaluates at compile-time to the following because of operator precedence :

*(pfruit.weight)

This is wrong because you are dereferencing a non-pointer value and trying to dereference a pointer with the . operator instead of -> .

EDIT :

Here's some piece of response and informations about pointers and to your question as I understood it.

#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);
}

在此处输入图片说明

once try this u may get it this can be written as pfruit->weight or (*pfruit).weight

   (*pfruit).weight;
product afruit;
product * pfruit;
pfruit = &afruit;

pfruit->weight; 

pfruit is a pointer and has the address of the struct afruit . We use pfruit->weight when we access struct members using a pointer .

When you use a struct name you use . operator. Now, pfruit is the pointer . Therefore, the struct itself is *pfruit

*pfruit.weight;

This is interpreted as *(pfruit.weight) , because . has higher precedence than * . and you can't use . operator with a pointer on LHS

You have to clearly show that you mean (*pfruit).weight

Bit late to the party, but try this:

It sounds like you are expecting values like 'int weight' to be stored as pointers to data somewhere else in the same way that the product is being referred to via pfruit. Some languages do do this, but in C++ simple data members like your weight and price are going to be stored actually in the product data structure. So, if afruit happens to exist at address 1000 in memory, then pfruit will have the value 1000:

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

This means that your weight (int) and price (float) data members will be stored here, one after the other. (and since an int and float are typically 32 bits in size, ie 4 bytes, the memory looks like this:

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

Consequently, pfruit also happens to point at the weight data (because it's first in the structure), but the price data is 4 bytes further along.

So, when you say you want to get to the data 'pointed to by weight' it doesn't make sense because it actually turns out that you don't need to because when you access the weight member you are already referring to the weight value. It's right there, in the memory occupied by afruit.

If you ARE trying to get a pointer TO the weight (or cost) data itself (instead of afruit, or actually from pfruit) you can do something like this:

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

There are now various ways to access the members of the product structure afruit:

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

I hope this helps someone, at least a little bit.

Sam

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM