0

Basically I have two functions with the main having a pointer which is pointing to c (variable memory address). My question is why I can't call the pointer from the print function and print out the string. In addition, what if I had a class file and wanted to call the pointer from there (without adding a parameter to the print function).

#include <iostream>
#include <string>
using namespace std;

static int print(){
    cout << *pointer;
}

int main() {
    string c = "Hello World!";
    string *pointer = new string;
    pointer = &c;
    print();
    return 0;
}
Vlad
  • 18,195
  • 4
  • 41
  • 71
sm15
  • 85
  • 3
  • 11
  • 5
    Learn about c++ scopes and smart pointers please and get a better c++ book – Angelus Mortis Dec 04 '15 at 16:47
  • 7
    Learning C++ by trial and error will not end well. Get a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn it systematically. – Baum mit Augen Dec 04 '15 at 16:47
  • [here](http://en.cppreference.com/w/cpp/language/scope) is a good reference on scopes – NathanOliver Dec 04 '15 at 16:49
  • I've always found http://www.cplusplus.com/ to be a useful site when it comes to getting a simple, straight up explanation of C++ syntax, templates, etc. Check it out. – soulsabr Dec 04 '15 at 16:50
  • 3
    @soulsabr http://en.cppreference.com/w/ is a better site in every possible way. – Etienne de Martel Dec 04 '15 at 16:56
  • pointer is a local variable, declared inside the main() function. That means it can only be accessed by other code below it inside the main() function. If you want it to be a global variable (note: generally a bad idea), you want to declare it outside of any function, e.g. just above your "static int print(){" line. – Jeremy Friesner Dec 04 '15 at 16:57
  • My thoughts were that the pointer was in memory (since I dont delete the pointer) and I'm able to access the pointer from different functions because the pointer is still in storage. – sm15 Dec 04 '15 at 17:00
  • 1
    @sm15 Yes, but the pointer is like any other variable, referred via a name, `pointer` in your case. That name belong to the scope of `main()`, so you cannot access it outside `main()`. – vsoftco Dec 04 '15 at 17:07

2 Answers2

5

You need to pass the pointer into the print function as it isn't a global.

static int print(string* pointer)

This should work just fine.

Also, you have a memory leak as your line

string *pointer = new string;

and call it using

print(pointer);

Allocates and never deletes the new string object. You might want to use

string *pointer = nullptr;

Instead. Also, using namespace <whatever> can lead to issues. Be sure you're doing it for good reasons and not to avoid typing a few extra characters.

EDIT :

As suggested here is a quick run down on scope.

The way to look at scope is kind of like the life of an object and who can see it. An object you create (not using new) has a scope up until it falls out of a block of code. Be that block a function, a conditional statement, or a loop. Further, the object can only be seen by code BELOW where it was created.

void foo()
{
  Bar b;

  /** really cool code **/
}

Here, object b of type Bar can be seen by the entire function. Once the function ends b "falls out of scope" and automatically has the destructor called on it. NOTE : This does not happen to pointers created with NEW.

void foo()
{
  Bar b1;

  for(size_t index = 0; index < 10; ++index)
  {
    /** some cool code **/
    Bar b2;
    /** some equally cool code **/
  }

  if(false)
  {
    Bar b3;
    /** some useless code **/
  }

  /** really cool code **/
}

Here, b1 can be seen by the entire function. b2 can only be seen by the "equally cool code" in the for loop. The "cool code" can't see it because it is declared below it. Finally, b3 never gets seen by anything because the conditional is always false. Once each object "goes out of scope", b1 at the end of the function and b2 at the end of the loop, the system takes care of calling the destructor.

That is the very basics of scope. NOTE : If you EVER use the keyword NEW to create a pointer to an object then you must DELETE that object somewhere. The scope only applies to the pointer (say Bar* b_ptr) and not the object created using NEW.

You would be wise to learn this well, grasshopper.

soulsabr
  • 895
  • 4
  • 16
2

why cant I call the pointer from the print function and print out the string

The inside of your print() function has no idea what *pointer is as it's declared in the scope of main() and not passed as a parameter. You CAN declare 'pointer' as a global variable, but why on Earth would you want to do that?

Why are you against passing pointer in as a parameter?

Declaration in that case would be

void print(string* pointer){
    cout << *pointer << endl;
}

and calling code would be simply

print(pointer);

Coincidentally, why have you made the return signature of print static int? void is fine. You're not returning any value from the function.

Also the "new" here is totally unnecessary

string *pointer = new string;

as the resource you will be pointing at already exists. "new" string does exactly that - creates space for a new string.

This is enough

string c = "Hello World!";
string *pointer = &c;
Lloyd Crawley
  • 606
  • 3
  • 13