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

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -