c++ - Accessing the value pointed by a pointer -
edit: question should considered abandoned. have flagged question deletion not know how proceed more @ point. thank of willingness , taking time out of day me.
i reading , following documentation @ cplusplus on data structures. have been trying figure out why compiler not accept " *pfruit.weight; " on own hours. sure simple missing.
error c2228: left of '.weight' must have class/struct/union 1> type 'product *' 1> did intend use '->' instead? "the operand left of period (.) not class, structure, or union."
so how correctly access value pointed pointer member?(not access reference)
#include <iostream> using namespace std; struct product { int weight; float price; } ; int main () { product afruit; product * pfruit; pfruit = &afruit; pfruit->weight; //access member of object have reference //equivalent to: (*pfruit).weight *pfruit.weight; //<------ sorry, meant problem, using reworded line of code example , isn't compiling , returning error. //access value pointed 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 operator precedence error. rules of c++ . has higher precedence *. *pfruit.weight correct if pfruit struct , weight pointer, in code it's other way around, pfruit pointer , weight int. need add brackets apply operators right way around
(*pfruit).weight;
Comments
Post a Comment