In a function how would I name a char an input variable and keep it?

Here is my code

void cString(String csn) {
  //Initializes a character array string
  char csn[2] = "A";
}

When I compile it I get the error message

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 cString(String)':

testMem:14:13: error: declaration of 'char csn [2]' shadows a parameter

char csn[2] = "A";

^

exit status 1

declaration of 'char csn [2]' shadows a parameter

How can I name this variable the contents of another variable, and if I can't do that what else can I do?
I would also like to keep it after the function ends, but I'm not sure if I could do that with return or not

I think..

You have : char csn[2] = "A";
You want : char csn[2] = 'A';

-jim lee

No, you have the String parameter “csn” and the char array “csn”.
That’s crazy.

What are you trying to do?

jimLee:
I think…

You have : char csn[2] = “A”;
You want : char csn[2] = ‘A’;

-jim lee

I don’t see anything wrong with char csn[2] = “A”;, per se.

char csn[2] = “A”; ← A’s a string ‘A’ is a char?

-jim lee

micahsuess:
How can I name this variable the contents of another variable, and if I can’t do that what else can I do?
I would also like to keep it after the function ends, but I’m not sure if I could do that with return or not

Do you mean that you want the name of the variable to be taken from the contents of the String? That cannot be done, variable names do not exist at runtime.

What is it that you are trying to do?

david_2018:
Do you mean that you want the name of the variable to be taken from the contents of the String? That cannot be done, variable names do not exist at runtime.

What is it that you are trying to do?

Yes, but is there still a way I can make variables (even if they're not real variables) or do I have to initialize them before I compile it

jimLee:
I think..

You have : char csn[2] = "A";
You want : char csn[2] = 'A';

-jim lee

No this is not the case, I want a string but in the example it is just 1 character

Still, you have not explained clearly what you are trying to do. Please explain step by step, an example would be great, too. "I want a string" isn't nearly enough unless you happen to be you.

I am not clear about what you want. what I caught you want a return value with string type data.
You can try with this function

const char* csn() {
  char *csn = "A";
  return csn;
}

and then you can convert to string with

  String csn_data=String(csn());

sorry if i misunderstood.

aarg:
Still, you have not explained clearly what you are trying to do. Please explain step by step, an example would be great, too. "I want a string" isn't nearly enough unless you happen to be you.

OK so first I was trying to make a function that turns a string into a character array using cstring as shown in

But then I needed to make a function that creates the cstring so info can be put into it afterwards, my problem is is that you can't create a variable after the code compiles, so I want to know an alternative

I am going to post another thread because I have another idea

jimLee:
char csn[2] = “A”; ← A’s a string ‘A’ is a char?

-jim lee

I don’t see what point you’re trying to make.
csn is big enough to contain the string “A”.

Oh!

Get the length of the incoming String

add one

Allocate a block of memory that side (Length + 1)

char* myCStr = (char*)&(buff[0]); // just to make sure

char* myCStr = (char*)buff; // would probably work as well.

write your chars into myCStr in a for loop.

for (int i=0;i<numChars;i++) {

myCStr = thar char from String i; // Never used String so don’t know how.
}
myCStr = ‘\0’; // Cap it with EOS.
-jim lee

TheMemberFormerlyKnownAsAWOL:
I don't see what point you're trying to make.
csn is big enough to contain the string "A".

"A" = A'\0'
'A' = A

Oh wait..

I was reading it.

char ast;

ast[2] = "A";

OK, your right.

-jim lee

write your chars into myCStr in a for loop.

There is no need for that, that function exists (both as part of the String class and as a cstring function, the String class version is :

char mycstring[myString.length() + 1];
myString.toCharArray(mycstring, myString.length() + 1);