Why won't my function script work?

Here is the code

void strToCharArr(String sn) {
String snan=sn+F("sa");
int sca=sn.length()+1;
sn.toCharArray(snan[sca],sca);
}

But whenever I run it I get this

Arduino: 1.8.13 (Windows Store 1.8.42.0) (Windows 10), Board: "Arduino Due (Programming Port)"

C:\Users*****\Documents\Arduino\testMem\testMem.ino: In function 'void strToCharArr(String)':

testMem:20:31: error: invalid conversion from 'char' to 'char*' [-fpermissive]

sn.toCharArray(snan[sca],sca);

^

In file included from C:\Users*****\Documents\ArduinoData\packages\arduino\hardware\sam\1.6.12\cores\arduino/Arduino.h:192:0,

from sketch\testMem.ino.cpp:1:

C:\Users****\Documents\ArduinoData\packages\arduino\hardware\sam\1.6.12\cores\arduino/WString.h:161:7: error: initializing argument 1 of 'void String::toCharArray(char, unsigned int, unsigned int) const' [-fpermissive]

void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const

^

exit status 1

invalid conversion from 'char' to 'char*' [-fpermissive]

Could someone explain what I'm doing wrong? I'm trying to make it so you can make a string array by only typing a string name. I'm running this on an arduino due, but I don't know if that is the problem.

Not really sure what you're trying to do, but the issue is that toCharArray expects a pointer to a char array as it's first parameter but scan[sca] is a char.

wildbill:
Not really sure what you're trying to do, but the issue is that toCharArray expects a pointer to a char array as it's first parameter but scan[sca] is a char.

There is no scan[sca] in my code so I don't know what you are talking about

I'm trying to tell it to make snan[sca] the character array

micahsuess:
I'm trying to tell it to make snan[sca] the character array

snan[sca] is a single char, not an array.

Also I would recommend you to not use Arduino strings in the future...

wildbill:
snan[sca] is a single char, not an array.

Do you know how I could make it go into an array?

TheUNOGuy:
Also I would recommend you to not use Arduino strings in the future...

Yes I know, that's why I'm trying to turn the string into a character array so I can \0 the string
But this is helpful

there is a library called PString which you can install from the library-manager.

PStrings are based on array of chars and bring back a part of the comofrt of strings but without the "eat-up-all-memory-problem"

here is a democode that extracts substrings from a long PString

#include <PString.h>
char    XML_Str_AoC[256] = " "; 
PString XML_Str_PS(XML_Str_AoC, sizeof(XML_Str_AoC));

char    XML_SubStr_AoC[256] = " ";
PString XML_SubStr_PS(XML_SubStr_AoC, sizeof(XML_SubStr_AoC));

char    Value_AoC[32] = " ";
PString Value_PS(Value_AoC, sizeof(Value_AoC));

char    IDStr_AoC[32] = "=";
PString IDStr_PS(IDStr_AoC, sizeof(IDStr_AoC));

char    Separator_AoC[32] = "=";
PString Separator_PS(Separator_AoC, sizeof(Separator_AoC));

unsigned long IDPos;
unsigned long ValuePos;
unsigned long EndOfValuePos;
unsigned long StrLen;

unsigned long BeginOfSubStr (char* p_PointerToSource, char* p_PointerToSubStr) {
  unsigned long result = strstr(p_PointerToSource,p_PointerToSubStr) - p_PointerToSource;
  return result;
}

unsigned long EndOfSubStr (char* p_PointerToSource, char* p_PointerToSubStr) {
  unsigned long result = strstr(p_PointerToSource,p_PointerToSubStr) - p_PointerToSource + strlen(p_PointerToSubStr);
  return result;
}

// my personal naming-convention parameter of functions start with prefix "p_"
void ExtractUntilSeparator(char* p_PointerToTarget, char* p_PointerToSeparator, char* p_PointerToSource)
{
  Serial.println("entering ExtractUntilSeparator");
  unsigned int LengthUntilDelimiter = strstr(p_PointerToSource,p_PointerToSeparator) - p_PointerToSource + 1;

  // attention use the command strlcpy with care. strlcpy perfoms a direct write into RAM at the given adress
  // the compiler has no chance to check if your adress makes sense
  // the writing is done mercyless to the given adress. This means wrong pointer-adress leads to unpredictable bugs
  strlcpy(p_PointerToTarget,p_PointerToSource,LengthUntilDelimiter);

  Serial.print("p_PointerToSource:>#");
  Serial.print(p_PointerToSource);
  Serial.println("#");

  Serial.print("p_PointerToTarget:>#");
  Serial.print(p_PointerToTarget);
  Serial.println("#");
  
  Serial.print("p_PointerToSeparator:>#");
  Serial.print(p_PointerToSeparator);
  Serial.println("#");
  
  Serial.println("leaving ExtractUntilSeparator");
}

// my personal naming-convention parameter of functions start with "p_"
void ExtractValueBehindSeparator(char* p_PointerToTarget, char* p_PointerToSeparator, char* p_PointerToSource)
{
  Serial.println("entering ExtractValueBehindSeparator");
  unsigned int PosOfSeparatorEnd = strstr(p_PointerToSource,p_PointerToSeparator) - p_PointerToSource + strlen(p_PointerToSeparator);
  // if separatorstring was  found   
  if (PosOfSeparatorEnd < strlen(p_PointerToSource) )
  {
    Serial.print("PosOfSeparatorEnd:>#");
    Serial.print(PosOfSeparatorEnd);
    Serial.println("#");
    unsigned int NoOfBytesUntilEoString = strlen (p_PointerToSource) - PosOfSeparatorEnd + 1; 
    
  // attention use the command strlcpy with care. strlcpy perfoms a direct write into RAM at the given adress
  // the compiler has no chance to check if your adress makes sense
  // the writing is done mercyless to the given adress. This means wrong pointer-adress leads to unpredictable bugs
    strlcpy(p_PointerToTarget, p_PointerToSource + PosOfSeparatorEnd, NoOfBytesUntilEoString);
  }
  else 
  { 
    p_PointerToTarget = ""; // if no separator was found there is nothing behind the separator
  }

  Serial.print("p_PointerToSource:>#");
  Serial.print(p_PointerToSource);
  Serial.println("#");

  Serial.print("p_PointerToTarget:>#");
  Serial.print(p_PointerToTarget);
  Serial.println("#");
  
  Serial.print("p_PointerToSeparator:>#");
  Serial.print(p_PointerToSeparator);
  Serial.println("#");
  
  Serial.println("leaving ExtractValueBehindSeparator");
}

void setup() 
{
  Serial.begin(115200);
  Serial.println();
  Serial.println("setup-Start");

  XML_Str_PS += "HTTP/1.0 200 OK ";
  XML_Str_PS += "Content-Type: text/xml ";
  XML_Str_PS += "Access-Control-Allow-Origin: * ";
/*
  XML_Str_PS += "X-Frame-Options: SAMEORIGIN ";
  XML_Str_PS += "X-Content-Type-Options: nosniff ";
  XML_Str_PS += "X-XSS-Protection: 1; mode=block ";
  XML_Str_PS += "X-Robots-Tag: none ";
  XML_Str_PS += "X-Download-Options: noopen ";
  XML_Str_PS += "X-Permitted-Cross-Domain-Policies: none ";
  XML_Str_PS += "Referrer-Policy: no-referrer ";
  XML_Str_PS += "Content-Length: 300 ";
  XML_Str_PS += "Connection: close ";
  XML_Str_PS += "Date: Fri, 19 Jun 2020 20:20:38 GMT ";
  */
  XML_Str_PS += "<?xml version='1.0' encoding='ISO-8859-1' ?><systemVariables><systemVariable name='Grube Stand' variable='3.000000' value='3.000000' value_list='' value_text='' ise_id='2632' min='0' max='65000' unit='cm' type='4' subtype='0' timestamp='1592598038' value_name_0='' value_name_1=''/></systemVariables>"; 

  Serial.print("XML_Str_PS#");
  Serial.print(XML_Str_PS);
  Serial.print("#");
  Serial.println();

  IDStr_PS = "'Grube Stand'";

  Serial.print("1: IDStr_PS#");
  Serial.print(IDStr_PS);
  Serial.print("#");
  Serial.println();

  IDPos = BeginOfSubStr(XML_Str_PS,IDStr_PS);
  Serial.print("IDPos:");
  Serial.print(IDPos);
  Serial.print("#");
  Serial.println();
  
  IDStr_PS = "value_list=";

  Serial.print("2:IDStr_PS#");
  Serial.print(IDStr_PS);
  Serial.print("#");
  Serial.println();
  
  ValuePos = BeginOfSubStr(XML_Str_AoC,IDStr_AoC);

  Serial.print("XML_Str_PS#");
  Serial.print(XML_Str_PS);
  Serial.print("#");
  Serial.println();

  Serial.print("ValuePos:");
  Serial.print(ValuePos);
  Serial.print("#");
  Serial.println();

  StrLen = ValuePos - IDPos; 
  Serial.print("StrLen:");
  Serial.print(StrLen);
  Serial.print("#");
  Serial.println();

//Grube Stand' variable='3.000000' value='3.000000'   
  // attention use the command strncpy with care. strncpy perfoms a direct write into RAM at the given adress
  // the compiler has no chance to check if your adress makes sense
  // the writing is done mercyless to the given adress. This means wrong pointer-adress leads to unpredictable bugs
  strncpy(XML_SubStr_PS,  XML_Str_PS + IDPos, StrLen);

  Serial.print("SubStr#");
  Serial.print(XML_SubStr_PS);
  Serial.print("#");
  Serial.println();

  XML_Str_PS = XML_SubStr_PS;
  Separator_PS = "value='";

  IDPos = EndOfSubStr(XML_SubStr_PS,Separator_PS);

  Serial.print("3: IDPos:");
  Serial.print(IDPos);
  Serial.print("#");
  Serial.println();

  StrLen = strlen(XML_Str_PS) - IDPos + 1;

  Serial.print("StrLen:");
  Serial.print(StrLen);
  Serial.print("#");
  Serial.println();

  strncpy(XML_SubStr_PS,  XML_Str_PS + IDPos, StrLen);

  Serial.print("SubStr#");
  Serial.print(XML_SubStr_PS);
  Serial.print("#");
  Serial.println();

  XML_Str_PS = XML_SubStr_PS;
  Separator_PS = "'";

  ExtractUntilSeparator(XML_SubStr_PS, Separator_PS, XML_Str_PS);
  Serial.print("value SubStr#");
  Serial.print(XML_SubStr_PS);
  Serial.print("#");
  Serial.println();
}

void loop() {
}

best regards Stefan

micahsuess:
Yes I know, that's why I'm trying to turn the string into a character array so I can \0 the string
But this is helpful

A string is a character array.