I am trying to write a piece of code to update a pointer with a function.
The function I am using is scalePointerUpdate. I have pasted below part of the code I am testing (but I can repost with more details if needed). The problem I have is that although the "currentSelection" variable updates correctly and is printed correctly, the pointer does not change when this variable changes. If I comment out "scalePointerUpdate (currentSelection);" line in the EnterSelection function i get the chrom scale printed out, otherwise I get the minor scale printed out.
Thank you! and yes clearly I still have a lot to learn, but that's the fun of it. I have included break; to each statement and it works.
I have actually used the switch statement for this function in the same way I did it for the other functions "getPresetSelectionName " and "getCurrentSelectionName " but for these function it works fine.
I learnt it from a post on the web suggesting to do this:
const char* getDayName(enum Days day)
{
switch (day)
{
case Sunday: return "Sunday";
case Monday: return "Monday";
/* etc... */
}
}
Why is it ok to do it in some case and not in others? I think I have my answer to this, in the first two functions for each case it returns a value, hence it exit the block. (?)
PS
Also I am not sure I understand why the type of this function is a constant. :-[
There are instances where a fall-through in a switch statement is desired. The results, if not intentionally coded, should only be considered lucky if they worked. The last assignment will take precedence.
An example of programmed fall-through is typical in command parsing;
case 'A':
case 'a':
// do stuff if 'A' or 'a' is detected
break;
And for your last question, it's not the function that's a constant, it's the return value. Why? Because that's the way whoever wrote the code decided the value should be treated. As the input to the function is an enum and enums are constants, it's likely that the return value is being used somewhere that requires a constant.
fluxia:
Why is it ok to do it in some case and not in others? I think I have my answer to this, in the first two functions for each case it returns a value, hence it exit the block. (?)
Yes, the 'return' statement prevents the 'case fall-through' (hint: you might want to Google that term).
Also I am not sure I understand why the type of this function is a constant. :-[
Because the function is returning a pointer to a c-string literal. It would be bad form to modify it via the pointer.
Hi again , I have read a bit about the c-string pointers and I suppose that in my case it would make more sense to use a non-const char array ... maybe...
I am posting my code below to explain better. I am basically using this pointer to update my LCD menu so the outcome of function getPresetSelectionName will update my string array presetSelectionPrint .
The code works fine but I wander if I am doing something weird because if I paste the code in Visual Studio I get a message that I cannot initialize a char* with a const char* - which obviously makes sense (and I also get messages about assigning INT to entities of the type created with the enum in the function "increaseSelection").
sorry.. here it is. I have taken out all the LCD stuff and commented out the parts which need the button library and it now compiles without any library needed
Thanks for your reply. Maybe I should have clarified that I am not concerned about all the print stuff in my code. That's just for me to monitor what's going on and I will remove it in my final code so I am not bothered if it is not elegantly written.
The code works as intended so there is no problem. I was just trying to clarify two issues:
1 - whether I should declare getPresetSelectionName as "const char*" or "char*"; they both seem to work fine.
2 - if it is ok to treat ENUM types as INTin the way I do (i.e.:
When in doubt, your first step should be to look to the compiler. Are you upsetting it? Your code produces warnings. If you try to compile it for a Teensy 3.2, it will throw errors. I guess different board packages have different levels of compiler tolerance specified.
I already told you in Reply #6 why it's a bad idea to assign a pointer to a c-string literal to a 'char *' pointer variable.
BTW, are you aware that these two lines are outside of any function? They are initializations and will only be applied once: