Printing a char* in C++

17,651

Solution 1

In the LongNumber constructor you declare a new local variable named number and initialize it with a new char array:

char* number = new char[digits+1];

Instead you should leave out the char*, so that it doesn't look like a new variable declaration and uses the object member variable:

number = new char[digits+1];

With the current code, the member variable number never gets initialized and using it later in print leads to an error.

Solution 2

Your number char* array would be out of scope when it exits the constructor. When you reach the print(), since the program no longer has access to the memory that *number is originally pointing to, it would crash (i.e. segmentation fault). To solve this problem do this instead:

class LongNumber
{
     char *number;
     LongNumber(const char *source);
     ~LongNumber();
     void print();
};

LongNumber::LongNumber(const char * source ){
        int digits = strlen(source);

        number = new char[digits+1];
        strcpy( number, source );    
}

void LongNumber::print(){
    cout<<number<<endl;
}

Don't forget to do the following:

LongNumber::~LongNumber()
{
    delete [] number;    // to avoid memory leaks
}

I would also highly recommend using STL::string rather than using char* for your *number variable, as you won't have to deal with memory management overhead yourself, and copying the string would be easier too.

Solution 3

It looks to me like your declaring number as a local variable. If you want to be able to call it again in another function, you have to declare it in the class definition... like so:

class LongNumber{
public:
        int digits;
        char* number;
        LongNumber(const char * source );
        void print();
}

LongNumber::LongNumber(const char * source ){
        digits = strlen(source);
        number = new char[digits+1];
        strcpy( number, source );
        //  cout<<number<<endl; - if the line is uncommented,
        //  the output is correct and there isn't a problem
}

void LongNumber::print(){
    cout<<number<<endl;
    // when I try to print with the same line of code here..it crashes
}

Hope that helps!!!

Solution 4

Your problem is in your constructor:

LongNumber::LongNumber(const char * source )
{
  ...
  // you are declaring a new local variable called `number` here
  char* number = new char[digits+1];
  strcpy( number, source );
  ...
}

You are not copying into the class member variable called number, you are declaring a new local variable in the body of the constructor and using that. The class member variable is unused, and probably is undefined. For a pointer member, this means the value could be any invalid value - then when you call print:

void LongNumber::print()
{
  cout<<number<<endl;
  // when I try to print with the same line of code here..it crashes
}

The number you are using here, is the class member variable, which as we said is undefined. The call to cout will then crash since it tries to use that invalid pointer.

The fix is to make the constructor use the correct class member variable:

LongNumber::LongNumber(const char * source )
{
  ...
  // use the class member variable `number` here
  number = new char[digits+1];
  strcpy( number, source );
  ...
}
Share:
17,651
TheMouse
Author by

TheMouse

Updated on June 04, 2022

Comments

  • TheMouse
    TheMouse almost 2 years

    I'm writing a simple program. There is only one class in it. There is a private member 'char * number' and two function (there will be more, but first these should work correctly :) ).

    The first one should copy the 'source' into 'number' variable (and I suppose somewhere here is the problem):

    LongNumber::LongNumber(const char * source ){
            int digits = strlen(source);
    
            char* number = new char[digits+1];
            strcpy( number, source );
            //  cout<<number<<endl; - if the line is uncommented,
            //  the output is correct and there isn't a problem
    
    }
    

    And a print function:

    void LongNumber::print(){
        cout<<number<<endl;
        // when I try to print with the same line of code here..it crashes
    }
    

    Sure, I'm missing something...but what?

    (As this is my first post...do you think the tags are corrected..how would you tagged the post?)

    Thank you in advance :)