0

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.

WhistlerQ
  • 3
  • 2
  • Possible duplicate of [How do I use arrays in C++?](https://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) – walnut Oct 02 '19 at 12:22
  • In particular read [this answer](https://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c/4810672#4810672) of the duplicate. Also consider using `std::array` if you really intend to have `nodeName` be a fixed-size char buffer or `std::string` if it is going to be used as (arbitrary length) string, as apparently the case in the `Serial.printl` statement. – walnut Oct 02 '19 at 12:56

2 Answers2

0

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.

  • 1
    The length of the `charName` array is 15, so having 16 as the third argument of `strncpy` is a bad idea. – jjramsey Oct 02 '19 at 12:48
0

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 (e.g., 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.

jjramsey
  • 1,131
  • 7
  • 17
  • 1
    There's no STL nor std::string on an arduino (avr-gcc), and I'd discourage the use of the Arduino String class. Besides strcpy / strncpy, there's a non-standard `strlcpy`, which helps in always adding the terminating '\0' in case of necessary truncations. – datafiddler Oct 02 '19 at 14:45