String dan Pointer C++

typedef struct
{
  int x, y;
  short life;
  char *nama;
} Man;

void setup() {
  Serial.begin(9600);

  //menggunakan 4 memori untuk string
  String hi = "Hello";
  String names = "World";
  String hello = hi + " " + names;

  //menggunakan 1 memori
  String greeting = hi;
  greeting.concat(" ");
  greeting.concat(names);



  String tempname = "Temperature";
  String temp = "23C";
  PrintVal1(tempname, temp); //membuat string redundan. Lihat fungsi
  PrintVal2(tempname, temp); //pass by reference, tidak redundan. Lihat fungsi

  char thestring[30] = "This is a string";
  //thestring = "New content"; //cara yang salah, panjang string harus sama!
  strcpy(thestring, "New content"); //good method

  char isgood[8] = "is good";
  //char allbad[38] = thestring + isgood; //cara yang salah, ini adalah operasi biner. gunakan strcat
  char allgood[38];
  strcat(allgood, thestring); strcat(allgood, isgood);



  char a[2] = "A"; char b[2] = "A";
  if (a == b) {}//cara yang salah, akan selalu bernilai false
  if (strcmp(a, b) == 0 ) {} //car yang benar, atau gunakan strcasecmp()


  //pointer
  int var = 0;
  int arr[] = {1, 2, 3};
  int *ptr;           //init, don't set to NULL (destroy), *ptr = 0, first address? no!
  ptr = &var;         //to address of var, not the value of var
  *ptr = 50;;         //pointer content is 50
  //ptr = 50;         //pointer (address) is 50
  //ptr++;            //pointer address + 1

  ptr = arr;         //to array
  ptr[0] = 50;      //array number 0, is 50
  ptr = &arr[0];     //equal to above, array number 0 is 50

  Man *ptrMan;
  Man manusia = {0, 0, 0, NULL};
  manusia.life = 9;       //kucing :D
  manusia.nama = "Todol"; //pointer in struct
  ptrMan = &manusia;
  ptrMan-> life = 0;         //mati
  
  char *pString = "This is text"; //not safe to change content! don't do!
  const char *pStringSafe = "This is text"; //display warning when change content

  char temperature[] = "23C"; //constant
  //Following syntax is not compiler friendly, it will be unhappy
  PrintValP("Temperature", temperature);
  //The following will make compiler happier (look at the fuction!)
  PrintValPC("Temperature", temperature);

  PrintString(temperature);
}


//make 2 new strings, redundant string
void PrintVal1(String tag, String value) {
  Serial.print(tag);
  Serial.print(" = ");
  Serial.println(value);
}

//pass by reference
void PrintVal2(String &tag, String &value) {
  Serial.print(tag);
  Serial.print(" = ");
  Serial.println(value);
}

//pass by pointer
void PrintValP(char *tag, char *value) {
  Serial.print(tag);
  Serial.print(" = ");
  Serial.println(value);
}
void PrintValPC(const char *tag, const char *value) {
  Serial.print(tag);
  Serial.print(" = ");
  Serial.println(value);
}
void PrintString(const char *str) {
  const char *p;
  p = str;
  while (*p) {
    Serial.print(*p);
    p++;
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}