I am trying to create separate values for each instance of an object. At this point I can give each of the 3 instances individual values for Ints, however I am not able to assign character arrays in the same way.
Ideally I would like to be able to assign separate character arrays and integers with each creation of an instance.
This is my current code
class node{
private:
int unitID;
int plantID;
int M1Thresh;
int M2Thresh;
char nodeName[15];
public:
node(int unitID, char nodeName[15], int plantID, int M1Thresh, int M2Thresh){
this->unitID = unitID;
this->plantID = plantID;
this->nodeName[15] = nodeName;
this->M1Thresh = M1Thresh;
this->M2Thresh = M2Thresh;
}
void Showit(){
// Tried this with no success also
// String outval = nodeName + "/0";
//Serial.print("Test string:");Serial.println(outval);
Serial.print("unit ID: ");Serial.println(unitID);
Serial.print("plant ID: ");Serial.println(plantID);
Serial.print("Name: "); Serial.println(nodeName[15]);
Serial.print("M1Thresh: ");Serial.println(M1Thresh);
Serial.print("M2Thresh: ");Serial.println(M2Thresh);
Serial.println(" ");
}
};
node Strawberries = node(101, "Strawberries", 01, 25, 25);
node Cucumber = node(102, "Cucumber", 02, 50, 50);
node Carrot = node (103, "Carrot", 03, 70, 70);
void setup(){
Serial.begin(9600);
}
void loop(){
Strawberries.Showit();
Cucumber.Showit();
Carrot.Showit();
delay(1000);
}
I would like to be able to create each instance with a separate char[].
My integers are sending/ assigning fine on the print out but the char[] print is giving me nothing.
Any help to point me in the right direction would me greatly appreciated.
Thanks.
Answering to you question (if i understood it right,) nodeName[15] it means accessing the 16 char position of the array. so you are only printing (I don't know what is Serial,print: so I assume it actually prints) the char on position 16:
Serial.print("Name: "); Serial.println(nodeName[15]);
this code doesn't seem right either:
this->nodeName[15] = nodeName;
You should use strncpy or something similar:
strncpy(this->nodeName, nodeName, 16);
Also, there is no guarantees that the nodeName will fit in 16 chars, so you should re think the way you are doing it.
Consider also using c++ features like std::sstring.
There are a couples problems in your use of nodeName
. First, this statement is wrong:
this->nodeName[15] = nodeName;
That assigns a value to the nonexistent (!) 16th character of this->nodeName
. And yes, I said 16th character, not 15th, since indexing in C++ is 0-based (eg, the first character is nodeName[0]
, second is nodeName[1]
, 15th is nodeName[14]
).
The value assigned to this->nodeName
is probably a pointer to the first character of nodeName
, converted to a char
, which is almost certainly not what you want. If you absolutely must use a character array for nodeName
, then you can do this:
std::strncpy(this->nodeName, nodeName, 15);
this->nodeName[14] = '\0'; // Ensuring NUL-termination, even if something
// is wonky with nodeName
However, I would highly second uneven_mark's recommendation to make nodeName
be of type std::string
. rather than a fixed-length character array. Dealing with strncpy
and its fellow fiends can be tricky.
The other problem in your code is here:
Serial.println(nodeName[15]);
Again, this prints the nonexistent (!) 16th character of nodeName
. If you use a character array for nodeName
, then you should just write
Serial.println(nodeName);
If you wisely have nodeName
be a std::string
, then you may need to write
Serial.println(nodeName.c_str());
instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.