Character array/string operations help

I’m having a bit of a problem understanding how strings/character arrays work. Here’s my code:

char number[]= "3312617101";

void setup()
{
  Serial.begin(9600);

  Serial.println("Started... ");
  Serial.print("number = ");  Serial.println(number); Serial.println();
 
}
    
void loop()
{
  
  char area_code[5];
  
  for(int index = 0; index < 3; index ++)
  {
    area_code[index] = number[index];
    Serial.println(area_code[index]);
    
  }
  
  area_code[3] = "7";
  area_code[4] = "U";
  area_code[5] = "\0";  

  Serial.print("area_code = ");
  Serial.println(area_code);
  Serial.println();

  for(int index = 0; index < 5; index ++)
  {
    Serial.println(area_code[index]);
    
  }
  Serial.println();
  
  delay (2000);
  
}

Basically, I’m trying to copy the first 3 numbers of the character array ‘number’ into a new character array called ‘area_code’, then adding other characters to that array. The output isn’t what I expect. I expect the output to be ‘3317U’, but instead the output is ‘33168 4’. What is happening here?

Thanks,
Randy

What is happening here?

You did not terminate the target array with a zero to make it into a string, area_code[5] does not exist and your assignments to the target array should be

  area_code[3] = '7';
  area_code[4] = 'U';
  area_code[5] = '/0';

your assignments to the target array should be

Except that the slash fell over the wrong way... 8)

There's always multiple ways to do this, but this is pretty simple:

char number[]= "3312617101";

void setup()
{
  Serial.begin(9600);

  Serial.println("Started... ");
  Serial.print("number = ");  
  Serial.println(number); 
}
    
void loop()
{ 
  char suffix[] = "7U";
  char phoneNumber[7];
  
  memset(phoneNumber, NULL, sizeof(phoneNumber)); // Fill string with NULLs
  strncpy(phoneNumber, number, 3);                // Add first 3 chars
  strcat(phoneNumber, suffix);                    // Tack on suffix

  Serial.println(phoneNumber);
  delay (2000);
}

To make it more interesting, why not accept the suffix letters from your keyboard and append those?

Except that the slash fell over the wrong way..

Pooh !

PaulS:
Except that the slash fell over the wrong way… 8)

No, my slash didn’t fall the wrong way. See here: https://www.arduino.cc/en/Reference/String

in the first example – char Str3[8] = {‘a’, ‘r’, ‘d’, ‘u’, ‘i’, ‘n’, ‘o’, ‘\0’};

it’s a backslash before the 0.

For some reason, its because I used double quotes, not single quotes.

My code:

char number[]= "3312617101";

void setup()
{
  Serial.begin(9600);

  Serial.println("Started... ");
  Serial.print("number = ");  Serial.println(number); Serial.println();
 
}
    
void loop()
{
  
  char area_code[5];
  
  for(int index = 0; index < 3; index ++)
  {
    area_code[index] = number[index];
    Serial.println(area_code[index]);
    
  }
  
  area_code[3] = '\0'; 

  Serial.print("area_code = ");
  Serial.println(area_code);
  Serial.println();
  
  delay (2000);
  
}

Works just fine.

If I use area_code[3] = “\0” it fails to give the proper result. If I use single or double quotes with a /0 it fails also.

Is there a difference between single and double quotes?

Most recently, I’ve been using python, which doesn’t care about single or double quotes, they’re the same thing.

Thanks for all the help and yes I have it figured out now, I just don’t understand the difference between double and single quotes. I will research that, but if someone can post about it, it would be helpful.

Randy

No, my slash didn't fall the wrong way.

UKHeliBob's did, though.

If I use area_code[3] = "\0" it fails to give the proper result.

It fails to compile. Talking about what code that doesn't compile would do is silly.

Is there a difference between single and double quotes?

Of course there is. Single quotes are for single characters. Double quotes are for strings.

PaulS: UKHeliBob's did, though.

oh, I missed that....

PaulS: It fails to compile. Talking about what code that doesn't compile would do is silly.

it complies for me and uploads to my arduino uno just fine, but gives the wrong results.

PaulS: Of course there is. Single quotes are for single characters. Double quotes are for strings.

Thank you for that.

I also don't mean to sound argumentive, just stating what I see happen.

Thanks alot, I think I have it sorted out now.

Randy

it complies for me

How? You appear to be trying to store a string in a char.

It's that >:( >:( >:( -fpermissive >:( >:( >:( that is still there in the compiler flags.

oqibidipo: It's that >:( >:( >:( -fpermissive >:( >:( >:( that is still there in the compiler flags.

Please explain.....

I'm very interested in learning the in and outs of programming these micro controllers.\

Thanks, Randy

revolt_randy: Please explain.....

I'm very interested in learning the in and outs of programming these micro controllers.\

Thanks, Randy

When the code is compiled, one of the arguments to the compiler is -f, with a value of permissive. That setting allows you to (try to) do stupid things like assign strings to chars.