Hi all.
The program fragment below try to compare two arrays and print some thing if true.
char *SDFoldersB0[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; //
char *CMDword[20] = {};
char *currtweekDAY[14] = {};
char timeStringWK[20];
strftime(timeStringWK, 20, "%A", timeinfo); // from RTC date got the current day of the week day;
currtweekDAY[0] = timeStringWK; // sign the day to
void currentDAY_compare() {
for (int i = 0; i < 7; i ++) {
if (currtweekDAY[0] == SDFoldersB0[i]) { //
currentDAYmark = i;
Serial.print("currentDAYmark =");
Serial.println(currentDAYmark);
}
}
}
the condition never true and never print any thing out.
if manually set: currtweekDAY[0] = "Friday"; works well.
got: currentDAYmark =4, i = 4;
int curretnDay = 100;
for (int ii = 0; ii < 7; ii++) {
int result = strcmp(currtweekDAY[0], SDFoldersB0[ii]);
if (result == 0) {
curretnDay = ii;
Serial.print("169currentDAYmarkM ="); //print: currentDAYmark =100, not calculated
Serial.println(curretnDay);
break; // it was this day, so no need to look at the rest of the week
}
}
if (curretnDay == 100) Serial.println("...currentDAYmark =100, not calculated");
I used an int instead of your character string assignment as I did not take time to see what you did that.
I could not test what I wrote, but I hope you see the idea.
Don't know what reason, may cause of the currtweekDAY[0] is come from: timeStringWK; ? as: currtweekDAY[0] = timeStringWK;
the compare: if (currtweekDAY[0] == SDFoldersB0[i]) never works.
but if I manually set: currtweekDAY[0] = "Friday"; , the above compare work well.
its why I got rid of the week day array SDFoldersB0[i] and just manually use strings like "Monday", "Tuesday" .... to do the compare, this way got what I need, just a foolish method.
It is hard to fix outside the context of a complete sketch.
Either post all your code, or better, a little tiny sketch you are writing to experiment with this kind of stuff… that gives you a compiler error or doesn't do what you think it should or want it to.
And perhaps describe carefully what you are trying to accomplish. You can def use an array of days of the week, you do not have to resort to "Monday" and copy pasting code six times!
I know I could read read read this code and then maybe be able to read your mind, but it would be better if you could just spare a few words and say what you are trying to accomplish with this sketch, or exactly, if it is just for illustrative purposes, what it does that it should not, or does not do that it should.
get the current day date of weekday, say today got: (timeinfo = localtime(&now);) = Saturday;
and set it to: currtweekDAY[0] = timeStringWK; (= Saturday);
use this currtweekDAY[0] compare with the week day array which included 7 week days, to get to know which week day match currtweekDAY[0] .
You already have the tm struct timeinfo, timeinfo.tm_wday will tell you the day of the week (Sunday is 0).
currtweekDAY[0] now contains a pointer to timeStringWK, which will not change regardless of the text stored in timeStringWK.
You are comparing pointers, currtweekDAY[] points to timeStringWK, SDFoldersBO[j] points to the literal strings that were declared when you initialized the SDFoldersBO array. Those will never be equal.
As was stated previously, you need to use strcmp() to compare the strings pointed to by the pointers, although there is no need to do that since you already know the numeric value for the day of the week.
Thank you david_2018.
I modified my reply #13 with a sorry there....
the reason I like to use the string compare is because there are some calculations based on that compare, let's say it take correntDAY as 0, and the timeinfo.tm_wday take Sunday as 0. I may modify my calculation for that.
actually, at the beginning, I designed it as: char *currtweekDAY[2] = {}; , some others just as: char *currtweekDAY[1] = {}; , the purpose is to use its pointer only for one, its why here is: currtweekDAY[0] .
I do have : char *SDFoldersB0[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; multi pointers, works as compare base like above.
I known there must be smart way there.
If you want one pointer to char, declare it. You don't need an array of them of which you ignore all but one:
char *pointerToChar;
Use it like you do currtWeekDSY[0];
pointerToChar = timeStringWK;
Of course name it something better.
Given, however, that your first problem was comparing string arrays improperly, I am suspicious that you just started improvising and threw stuff at the compiler until it shut up.
You probably used '=' to try and copy character arrays, we would have told you that like strcmp(), copying needs to be done differently and turned you on to strcpy(),
This is not an effective development path.
You don't need Strings or strings or pointers to char, you've been offered the simple solution of dealing with the day of the week as a number, unless and until you need to present it to humans, for which
// assume int dayNumber is from the RTC
Serial.println(SDFoldersB0[dayNumber]);
will work fine. I might name that SDFoldersB0 something like dayNames. You do you on the naming stuff.
There are a handful of standard functions for working with character arrays. They are not as easy to use as Capital S Strings, but you can figure it out.
You just have to take a break from getting them to work for you in the context of a real project and find some learning materials and start as if you know even less than you think.