How to inicialise a variable char array in Arduino

I need to initialize this variable inside the loop in Arduino, how can I do?

Here in my code but none works

char buffer[100];

void setup() {
  Serial.begin(115200);
  .
  .
}

void loop() {
.
.
  buffer[] = Null;
  buffer[] = "";
  buffer[] = " ";
.
.
}

char buffer[]="something";

or

char buffer[100];
buffer[0]='s';
buffer[1]='o';
...

or use functions like strcpy() or memcpy().

Your variable is global so it is initialized to zeroes for you. Did you want some different value?

If you want to set it BACK to an empty string:
buffer[0] = '\0';

Which of these codes is the best and effective method to initialize a char array?

buffer[0] = '\0';
strcpy( buffer, "");
memset( buffer, NULL, sizeof(buffer));

For local array - this one:

or
memset( buffer, 0, sizeof(buffer));
if you need to clear it contents explicitly.

Global arrays initialized automatically by compiler

buffer[0] = '\0';

Unless you are doing weird non-standard things with the array that you have chosen to not share.

This answer follows directly from the standard termination method, if you don't understand why, please study C strings, any reasonable tutorial will explain it.

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