LCD4Bit arrays & pointers confusion

Hi All,

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.

char msgs[6][15] = {"apple", "banana", "pineapple", "mango", "watermelon", "pear"};

So we've setup a 2d array containing the above, no problem.

Then...

  int pick = random(NUM_MSGS);
  char* msg = msgs[pick];
   lcd.clear();
  lcd.printIn(msg);

I think i've sussed this out in the process of writing this post.

Am I right in thinking you can't pass an array to a function in c++ so we have to create a pointer to the array and pass that instead? :o

Hi,

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].

Mike

Why not just pass the adress of the element in question?

char msgs[6][15] = {"apple", "banana", "pineapple", "mango", "watermelon", "pear"}; 

lcd.clear();
lcd.printIn(msgs[random(NUM_MSGS)]);

This should work. :slight_smile:
[edit]When passing arrays you are passing a pointer.[/edit]

Ah I see what you did there :wink:

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. :slight_smile:

So let em see if I have this right...

You can pass a pointer to a function and define another pointer like so...

void main(){
char test[2][3] = { "12", "34" };
char *pointer;

pointer = test[1];
myfunction(pointer);
}
void myfunction(char *anotherptr){
//stuff
}

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.

Cheers.

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.