Convert char string into char array

Hello I am interfacing the arduino with PC.

But when I am receiving data from PC to arduino, I saves the multiple characters in same variable.

If 'A1' is sent from the PC, the whole thing gets saved in defined variable only (using this "var = Serial.read()")

I want to save both terms in two different variables or an array (I want it to be like this, var[0] = 'A' and var[1] = '1')

M sorry if this question sounds nweb, I googled this thing but could'nt get anything helpful.

Hello,

bparth:
If 'A1' is sent from the PC, the whole thing gets saved in defined variable only (using this "var = Serial.read()")

This is not possible because Serial.read() only read one character.

A "char string" is already a char array. Post your code.

Hey brother check this out

Looks like there is a method that will do this that is part of the standard string functionality.

maybe something like this

String myString = "";
while(Serial.available()){
   myString += (char)Serial.read();
}
char myCharArray[myString.length()];
myString.toCharArray(myCharArray,myString.length());

That might work, but it's a rather excessive approach. As guix pointed out, Serial.read() returns one character at a time already. It's not technically a "char", it's a signed 16-bit integer because it returns -1 if there's no data. You could do something like this:

char mystring[STRLEN_MAX];
uint8_t i = 0;

while (Serial.available() && (i < STRLEN_MAX)) {
  mystring[i] = Serial.read();
  i++;
}

Alternatively, you can skip the available check:

int rcvd;
char mystring[STRLEN_MAX];
uint8_t i;

for (i = 0; i < STRLEN_MAX; i++) {
  rcvd = Serial.read();
  if (rcvd < 0) break;  // Bail out if there's no data
  mystring[i] = (char)rcvd;
}