/*
*
* test to combine two char
*/
// Codes
const char code_Array[][7] = {"J00A", "J00B", "J00C", "J00D", "J00E", "J00F", "J00G", "J00H"};
char controller;
const int numberArray = 8;
// TIME
const unsigned long SamplePeriod = 500; // sampling period in milliseconds
void setup() {
Serial.begin(115200);
Serial.println("Starting...");
}
void loop() {
static unsigned long previousTime = 0;
unsigned long currentTime = millis();
if (currentTime - previousTime >= SamplePeriod) {
previousTime = currentTime;
mytest;//<----------
}
}
void mytest() {
for (int x = 0; x < numberArray; x++) {
strcat(controller , code_Array[x]);
strcat(controller, '1z');
Serial.println(controller);
}
}
I want the value of controller to be the code_Array[x] + '1z'
I have looked at strcat and strcpy but cannot seem to get the right code.
Many thanks in advance
‘1z’ you can only us a single character.
Did you forget "()"?
strcat, strcpy, etc all operate on ARRAYS of char. Your controller variable is a SINGLE character, so cannot hold more than one, single character.
' ' for single character, " " for multiple characters (string).
You are misusing strcat.
Check prototype: char *strcat(char *dest, const char *src);
Your var controller is of type char. To make a "robust code", you would have to :
- change
controllerto type char * (or char[] if you are sure about length) - allocate enough space in
controller - reset
controllerat the beginning of each line.
However, a far much simpler way to do this is to use
sprintf(controller, "%s1z", code_Array[x]);
I think you're addressing the wrong person...
It's highly possible I missed something, but what I see is that controller is char in the sample, I don't know how you can expect to write a full text in a single byte ![]()
I did.... Dahme. But it does not compile.
Would I have to have two lines of strcat or is there another way.
You must declare the protoype before using it
Change:
char controller;
to
char controller[10];
Change:
strcat(controller , code_Array[x]);
strcat(controller, '1z');
to
strcpy(controller , code_Array[x]);
strcat(controller, "1z");
/*
*
* test to combine two char
*/
// Codes
const char code_Array[][7] = {"J00A", "J00B", "J00C", "J00D", "J00E", "J00F", "J00G", "J00H"};
const int numberArray = 8;
// TIME
const unsigned long SamplePeriod = 500; // sampling period in milliseconds
void setup() {
Serial.begin(115200);
Serial.println("Starting...");
}
void mytest();
void loop() {
static unsigned long previousTime = 0;
unsigned long currentTime = millis();
if (currentTime - previousTime >= SamplePeriod) {
previousTime = currentTime;
mytest();//<----------
}
}
void mytest() {
for (int x = 0; x < numberArray; x++) {
// Method 1
char controller[7]; // 4 letters + 2 letters (1z) + NUll char
memset(controller, 0, sizeof(controller));
strcat(controller , code_Array[x]);
strcat(controller, "1z");
Serial.println(controller);
// Method 2
char controller2[7]; // 4 letters + 2 letters (1z) + NUll char
snprintf(controller2, 7, "%s1z", code_Array[x]);
Serial.println(controller);
}
}
And be aware that your title and description is not correct. You don't want to combine to chars but two char arrays
Thank you all - I made the changes susggested and here is the correct code.
/*
*
* test to combine two char
*/
// Codes
const char code_Array[][7] = {"J00A", "J00B", "J00C", "J00D", "J00E", "J00F", "J00G", "J00H"};
char controller[10];
const int numberArray = 8;
// TIME
const unsigned long SamplePeriod = 500; // sampling period in milliseconds
void setup() {
Serial.begin(115200);
Serial.println("Starting...");
}
void loop() {
static unsigned long previousTime = 0;
unsigned long currentTime = millis();
if (currentTime - previousTime >= SamplePeriod) {
previousTime = currentTime;
mytest(); //<----------
}
}
void mytest() {
for (int x = 0; x < numberArray; x++) {
strcpy(controller , code_Array[x]);
strcat(controller, "1z");
Serial.println(controller);
}
}
Why not?
actually two cStrings (ie null terminated char arrays)
the IDE does it for you... (but it's a good idea to learn to do it the right way)