string v String

I have the following code snippet that works

    String payloadAsString = "";
        for (int c = 0; c < payloadLength; c++) {
          payloadAsString += (char)payload[c];
        }
     
        Serial.println(payloadAsString);

changing to this

   char payloadasstring[10];
        
        for (int c = 0; c < payloadLength; c++) {
          payloadasstring += (char)payload[c];
        }
     
        Serial.println(payloadasstring);
 Serial.println();

I get the following message

error: incompatible types in assignment of ‘char’ to ‘char [10]’

im basically trying to replace the use of String

          payloadasstring += (char)payload[c];

You are trying to increment the address of an array. Does that make sense?

The String class overloads the += operator to give it special meaning.

          payloadasstring[c] = (char)payload[c];

Though, if you have payload as an array already, making a copy of it is rather pointless.

Instead of showing us how you are trying to do something, perhaps you should explain what you are trying to do. A simple cast may be all that is needed.

I am just starting the chapter on arrays.
That example is from a prog that I have which works but crashes after a while.
I am thinking that String is at fault so wish to convert it to a char type and print it out.

Boardburner2: I am just starting the chapter on arrays. That example is from a prog that I have which works but crashes after a while. I am thinking that String is at fault so wish to convert it to a char type and print it out.

The chapter on arrays in what book?

Perhaps showing all the code, using Strings, would be good. There may be ways to accomplish what you want with Strings. Though changing to strings is certainly a better idea.

The change that I showed should be all that you need to change.

C++ for dummies was all I could find.
I have Pohl on backorder.
this is from an NTAG reader

#if 0
#include <SPI.h>
#include <PN532_SPI.h>
#include <PN532.h>
#include <NfcAdapter.h>

PN532_SPI pn532spi(SPI, 10);
NfcAdapter nfc = NfcAdapter(pn532spi);
#else

#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>

PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);
#endif

void setup(void) {
  Serial.begin(9600);
  Serial.println("NDEF Reader");
  nfc.begin();
}

void loop(void) {
  Serial.println("\nScan a NFC tag\n");

  if (nfc.tagPresent())
  {
    NfcTag tag = nfc.read();
    Serial.println(tag.getTagType());
    Serial.print("UID: ");Serial.println(tag.getUidString());

    if (tag.hasNdefMessage()) // every tag won't have a message
    {

      NdefMessage message = tag.getNdefMessage();
      Serial.print("\nThis NFC Tag contains an NDEF Message with ");
      Serial.print(message.getRecordCount());
      Serial.print(" NDEF Record");
      if (message.getRecordCount() != 1) {
        Serial.print("s");
      }
      Serial.println(".");

      // cycle through the records, printing some info from each
      int recordCount = message.getRecordCount();
      for (int i = 0; i < recordCount; i++)
      {
        Serial.print("\nNDEF Record ");Serial.println(i+1);
        NdefRecord record = message.getRecord(i);
        // NdefRecord record = message[i]; // alternate syntax

        Serial.print("  TNF: ");Serial.println(record.getTnf());
        Serial.print("  Type: ");Serial.println(record.getType()); // will be "" for TNF_EMPTY

        // The TNF and Type should be used to determine how your application processes the payload
        // There's no generic processing for the payload, it's returned as a byte[]
        int payloadLength = record.getPayloadLength();
        byte payload[payloadLength];
        record.getPayload(payload);

        // Print the Hex and Printable Characters
        Serial.print("  Payload (HEX): ");
        PrintHexChar(payload, payloadLength);

        // Force the data into a String (might work depending on the content)
        // Real code should use smarter processing
        String payloadAsString = "";
        for (int c = 0; c < payloadLength; c++) {
          payloadAsString += (char)payload[c];
        }
        Serial.print("  Payload (as String): ");
        Serial.println(payloadAsString);

        // id is probably blank and will return ""
        String uid = record.getId();
        if (uid != "") {
          Serial.print("  ID: ");Serial.println(uid);
        }
      }
    }
  }
  delay(3000);
}

your suggestion works but seems to ignore the 10 char specified size
it will print out records longer than 10 characters as nomal, does this mean memory is getting overwritten outside the defined array ?
I intend to limit to 8 char in prog anyway but that was unexpected.

In the absence of a memory management unit, you can write wherever you wish via pointers, so it's up to you to ensure you don't write to stuff you don't own.

your suggestion works but seems to ignore the 10 char specified size it will print out records longer than 10 characters as nomal, does this mean memory is getting overwritten outside the defined array ? I intend to limit to 8 char in prog anyway but that was unexpected.

I'm not sure what you mean by this. The payload that you are getting is not a NULL terminated string. You assume that it is when you make the copy, and you don't add a NULL at the proper place.

You know the proper place. It's defined by the payloadLength variable.

Understood thanks, i was wondering if the array structure itself would generate a runtime error if its size was exceeded. I suppose it would if i overwrote something important though. Just getting to grips with pointers.

When i said i intended to limit to 8 in prog, it was the pointer count i meant.

Edit. Runtime error, im sure i used to get these with turbo pascal, although i used to associate it with interpreted languages.

i was wondering if the array structure itself would generate a runtime error if its size was exceeded.

No.

I suppose it would if i overwrote something important though.

No. You will get weird behavior, though.

When i said i intended to limit to 8 in prog, it was the pointer count i meant.

It really helps if you talk about specific examples from your code.

Thanks ill try and remember that next time.

If your goal is to write code for an Arduino board and you don't know either C or C++, why not back off and start with C, which is an easier first step. Mastering C will make the step to C++ easier. Also, you will quickly see that using the String class in a C program is often an H-bomb-to-kill-an-ant approach to the problem and likely consumes more resources than need be.

I agree with econjack, but... If he don't know C++ how did he understand the libraries that he found in the internet?

I think the statement:

          payloadasstring += (char)payload[c];

says it all. If payloadasstring is a char array, why would you even try to alter a constant lvalue?

Book by Pohl im waiting for is C not C++. Just going by what i have.

Thought id try going through a largely uncommented library and try to comment what it is doing line by line.

I think i have the basics of classes and instances although overloading is a bit beyond me.

Payloadasstring was authored as a String. Changing it to an array was an attempt to cure occasional crashes which appears to have worked. Reason for copying the array was i understand that serial.println requires a null terminated string which i have to append myself. Perhaps wrongly.

+=. =

I failed to spot that until it was pointed out. Im still struggling with a lot of the syntax.