I have this a class called PPString:
PPString.h
#ifndef __CPP_PPString
#define __CPP_PPString
#include "PPObject.h"
class PPString : public PPObject {
char *stringValue[];
public:
char *pointerToCharString();
void setCharString(char *charString[]);
void setCharString(const char charString[]);
};
#endif
PPString.cpp
#include "PPString.h"
char *PPString::pointerToCharString() {
return *stringValue;
}
void PPString::setCharString(char *charString[]) {
*stringValue = *charString;
}
void PPString::setCharString(const char charString[]) {
*stringValue = (char *)charString;
}
I’m trying to set the stringValue using std::cin:
main.cpp
PPString myString;
myString.setCharString("LOLZ");
std::cout << myString.pointerToCharString() << std::endl;
char *aa[1000];
std::cin >> *aa;
myString.setCharString(aa);
std::cout << myString.pointerToCharString() << std::endl;
The first one, which uses a const char works, but the second one, with a char doesn’t, and I get this output:
copy and paste from STDOUT
LOLZ
im entering a string now...
Bus error
where the second line is what I entered, followed by pressing the return key.
Can anyone help me fixing this? Thanks…
The setCharString with the
char *s[]signature is dereferencing the first element of an array of pointers tochar*. It has not been allocated. If you change the declaration ofaatochar aa[1000];, it will probably run.There are some other issues too (also pointed out by others). The assignment to the variable
stringValueis also dereferencing memory that does not appear to have been allocated. It’s hard to say what the usage is, but it should maybe not have the[]declaration. In addition, the assignment is storing a pointer to stack memory, which will likely not be valid after another function call.