I'm writing my own little sketch from scratch to write to a LCD, I'm doing it from scratch to try and fully understand exactly whats happening at what point. Rather than relying on libraries and then have no clue how things have been done.
Doing good so far however I got a little confused.
it is always a good idea to pass the address of the array (a pointer to it) to the function, otherwise you need a second array of the same size as local parameter and it takes a lot more time to copy the contents of one array to the other - but you can do if you want.
Another way so save space and simplify things (no good programming practice - but a good solution for embedded designs) is to use a global array and pass only the array index to the function lcd.printIn and rewrite the function printIn so that it accesses msg[index].
I probably should of been more clear by saying I was reading through the LCD4bit library at the time making sure I understood everything, which is where I got the code from.
Well thanks for the comments, the good news is that I am learning and it is going in there and not just coming straight back out again.
You can pass a pointer to an array in a function. Which changes the function to:
void myfunction(char myarray[])
{
//stuff
}
You can pass test[1] to the function above, define test as a global array and send just the index we require or pass the array to a pointer in myfunction :-?
I hope this is all making sense! It works in my head I just might not be explaining it very well.
Are any of these bad practice? If so why?
Mike mentioned that a global array is bad practice (except for embedded devices), is this because it ties up more memory? Also that copying an array is slower which makes sense.
a global array is bad practice (except for embedded devices), is this because it ties up more memory?
It is bad practice even for embedded devices. It is bad because other functions can access it without the owner knowing anything about it. This is generally less important in an embedded system because they tend to run only one thread but nevertheless only do it if you have to. In fact it tyes up less memory but is considered bad coding practice.