NRF24L01 Arrays or Strings

I am learning to code the NRF24L01 transceiver module and came across some new commands.
I have tried to search for these but I couldn't find a clear answer.

Transmitter:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(10, 9); // CE, CSN

const byte address[6] = "00001";

void setup() {
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}

void loop() {
  const char text[] = "Hello World";          //<-----
  radio.write(&text, sizeof(text));           //<-----
  delay(1000);
}

Receiver :

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(8, 7); // CE, CSN

const byte address[6] = "00001";

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    char text[32] = "";                      //<--------
    radio.read(&text, sizeof(text));        //<--------
    Serial.println(text);
  }
}

In the transmitter:
I don't get why we have created the text with these two empty [] when sending the Hello World.

In the receiver:
And I don't get why we have created the text with the [32], while in the transmitter we have created it with empty [].

And I don't get why we have used the & before the text in the radio.write command in both the transmitter and receiver.

That is basic C++ array allocation/initialization/usage.

You can get a meaningful length from the initializer, so you don't have to size the array explicitly.
The packet will be automatically expanded to 32 bytes in your configuration.

On the receiving side, you only know that 32 bytes will be received,
so you have to allocate a buffer for the packet.

The & is needed for non-array elements to get their address,
it is not needed for an array, that is also a pointer to its start.

You should read a book or a tutorial about C++.

It lets the compiler figure out the appropriate array size ... it won't make a mistake.

There's a subtle difference when using arrays as function parameters:

  char myArray[32];

  myFunction1(myArray);   <----- myArray decays to a 'char *'

  myFunction2(&myArray);    <----- myArray decays to a pointer to a char array of length 32

If used as a parameter to a void*, I don't see the difference.

In that case, correct since the 'void *' could only be legally recast and dereferenced inside the function as a 'uint8_t *'. So all pointer math would be in terms of bytes, not chunks of 32 bytes.

İts like hearing some magic @Whandall , @gfvalvo .
What book or source do you suggest to learn these stuff?

This seems ok

I/O is done there in the standard way, but there is no cout or cin in Arduino,
so the examples may not be directly usable.

I wouldn't recommend the one from tutorialspoint at all: It is terribly outdated, promotes questionable practices, and parts of it are plain wrong.
I'd recommend learncpp.com instead.

I had to pick one from the search, I learned from the original books.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.