trying to assign an address for Ptr, get cannot convert 'char (*)[10]' to 'char*

Hello again folks,

Well, I've been scolded for no address pointers and strcatting them and the like probably causing a SF or something... so I thought I had the solution to declare an char array for a char pointer and use that address:

Here is one function when I have two pointers and two char array addresses... but where am I going wrong?

void beepRepeatSet() {
  //BeepRepeatSet: Add 1x000 where x is 2^(x-1) times to repeat
  char *hertzA; <--- DECLARE ptr
  char h[10]; <--- DECLARE char array
  char r[10];
  hertzA = &h; <-- Assign address of h to hertzA results in error...
  char *hertzR;
  hertzR = &r;
  int hertzI, hertzLen;

hertzA = reinterpret_cast<char *>(hertz); //Convert long to char*
  hertzLen = strlen(hertzA);
  hertzR = hertzA;
  hertzI = atoi(hertzR);
  beepRepeat = 2 ^ (hertzI - 1);
  if ((hertzI < 2) || (hertzLen < 6)) beepRepeat = 0;
  //Serial.println(beepRepeat);
}

When you want a char * pointer to point to the beginning (element 0) of a char [] array, no & operator is necessary

hertzA = h;

When you want it to point to a specific element with index i, you can use either

hertzA = &h[i];

or

hertzA = h + i;

This also means that the original assignment can also be expressed as

hertzA = &h[0];

What you did in your code forms a pointer to the entire array, not a pointer to a specific element. Such pointer is incompatible with char * type. Hence the error.

In your code you are resorting to reinterpret_cast to defeat the language's type system and "fix" the incorrect use of & operator. Don't do this.


It is not clear what you are doing in your function though and whether you even need those pointers. So far you seem to be working with uninitialized (garbage) arrays.

Montmorency, thank you for your assistance with these coding woes of mine.