C++ Pointers

In the Following Code :

#include <iostream>

int main(){
    std::string name = "My Name Is Krish";
    
    std::string *pointer_for_name = &name;
    
    std::cout<<"The string is : "<<name<<"\n";
    
    std::cout<<"The memory adress is : "<<&name<<"\n";
    
    std::cout<<"The memory adress through the pointer is : "<<pointer_for_name<<"\n";
    
    return 0;
}

Why do we store memory address of name in pointer_for_name. Instead we can just print it using &name?
Thank You

At the moment it’s just showing you that the tool exists. The uses are a little bit harder to explain succinctly. Try this conversation to see some scenarios where pointers make sense.

1 Like