Help with function

Hello,

I am trying to create a function:

char CustomPrint(char Button, int PWMVal){

  Serial.print("~");
  Serial.print(Button);
  Serial.print(": ");
  Serial.print(PWMVal);
  Serial.print("#");

}

The char Button is declared as following:

const char BtnU[] = "~ButtonUp#";

I get this error:

error: invalid conversion from 'const char*' to 'char'

When I use:

CustomPrint(BtnU, 10);

Sorry for all the questions.... I am also reading a book on C programming :), but this pointer thing and char conversion still is confusing me a bit.

Cheers

You are passing a "const"ant string into a function expecting a non-constant string.

Try changing :

char CustomPrint(char Button, int PWMVal){

to

void CustomPrint(const char *Button, int PWMVal){

I also changed the return value to void because you weren't returning anything from within CustomPrint, but the const char was the change to get rid of the error you pointed out.

Thanks SpinLock, the code works partially.

I've changes the code to:

void CustomPrint(const char *Button, int PWMVal){

  Serial.print("~");
  Serial.print(*Button);
  Serial.print(": ");
  Serial.print(PWMVal);
  Serial.print("#");

}

Now it prints:

~:10#

Instead of:

~ButtonUp: 10#

Whats wrong?

Thanks

You also managed to change:
from:

Serial.print(Button);

to:

Serial.print(*Button);

Put it back and it will work as you need.

Ok this solves the problem :slight_smile: I would like to know why this should be used (I want to learn something out of it, instead of just asking and copying code)

I don't get why it should be:

const char *Button

and especially the "*"?

I mean, the variable is defined as:

const char

Thanks for the help :slight_smile:

Actually, it is declared as :

const char BtnU[] = "~ButtonUp#";

or "an array of constant chars"

Let's ignore the const for now, it just signals the compiler to check certain operations on the variable. const generally means, you can read the thing, but never assign to it. It is very good practice to use const on functions that don't change variables, and will help with debugging later. (the compiler joins in and helps...)

In C, you can pass an array as a pointer,

void print(char *data)
print(BtnU);

and in your example, when you called print with Button, you are doing just that.

In the case of the Serial class, the compiler knows you are passing a pointer and handles it by calling the print(char *) which prints the data pointed to until it reaches a terminating NULL. The compiler automatically throws in a NULL for you automatically when you declare:

const char BtnU[] = "whatever";

Now, when you call print(*BtnU); you are saying, call print with the "dereferenced" pointer. The code then does something like: pass the value at the memory location pointed to by BtnU to the print function.

I think what you are attempting to practice is to pass variables as you declared them, which isn't what you want to do in C.

I recommend reading Pointers: Understanding Memory Addresses - The Basics of C Programming | HowStuffWorks for more information on pointers and C. The same principles also apply for C++, but start here first.