sm15
sm15

Reputation: 85

How do I print the value of a pointer from within a method?

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;
}

Upvotes: 0

Views: 3252

Answers (2)

soulsabr
soulsabr

Reputation: 904

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.

Upvotes: 5

Lloyd Crawley
Lloyd Crawley

Reputation: 616

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;

Upvotes: 2

Related Questions