c++ - Pointer Assignment Clarification -
c++ - Pointer Assignment Clarification -
still learning c++ here. trying understand pointer assignments. question have in comments of code below.
#include <string> #include <iostream> int main(){ std::string test = "foop"; std::string * pointer; *pointer = test; //why crash program... pointer = &test; //but doesn't? homecoming 0; } based on i've read thought *p = o , p = &o did same thing. appreciate enlightenment.
thank you!
*p = o;
assigns o thing p points to. in code p (or pointer) uninitialized, assigns god-knows-what, causing crash (if lucky), or silently corrupting memory (if aren't).
p = &o; assigns address of o p, making p point o. well-defined.
c++ pointers segmentation-fault
Comments
Post a Comment