Problem with string,char

Hi i am having a problem declaring a char character.

In the first photo its the error that i have. In the next photos its how i declare my variable and i how i give it values.

Thanks in advance

2.JPG

3.JPG

I guess you know how to copy and paste?

So copy your errors and code and post them here in a post, not as a bloody pictures. And read How to use this forum - please read., specifically point 7 on how to post code and error messages.

Character arrays are initialised using double quotes, not single quotes.

Please do not post screenshots. Rarely someone minds do download and view them and copy-paste from images are impossible so if anyone should answer, they would have to write the text of the images - not cool! :slight_smile:

i am having a problem declaring a char character.

A char variable can only hold one character. You can, of course, have an array of chars if you want to hold multiple characters

locat[255] is the 256th element of the locat array which does not exist. Even if it did it could only hold one character

Time for you to read up on arrays I think

char locat[255];
if (StDir==1)
      {
         lat2d=41.140636;
         lon2d=24.910648;
         Haversine();
         if (distance<0.4)
         {
            StDir=2;
            locat[255]='Entry of the University';
            // Αποθήκευση σε αρχείο txt του locat
            Serial.print(distance);
         }
      }
      else if (StDir==2)
      {
         lat2d=41.140329;
         lon2d=24.913801;
         Haversine();
         if (distance<0.4)
         {
            StDir=3;
            locat[255]='Electrical Engineers';
            // Αποθήκευση σε αρχείο txt του locat
            Serial.print(distance);
         }
      }
      else if (StDir==3)
      {
         lat2d=41.143937;
         lon2d=24.917577;
         Haversine();
         if (distance<0.4)
         {
            StDir=4;
            locat[255]='Roundabout';
            // Αποθήκευση σε αρχείο txt του locat
            Serial.print(distance);
         }
      }
      else if (StDir==4)
      {
         lat2d=41.145758;
         lon2d=24.915430;
         Haversine();
         if (distance<0.4)
         {
            StDir=5;
            locat[255]='Dormitories';
            // Αποθήκευση σε αρχείο txt του locat
            Serial.print(distance);
         }
C:\Users\Chrysostomos\Desktop\Problem_with_char\Problem_with_char.ino:175:23: warning: overflow in implicit constant conversion [-Woverflow]

             locat[255]='Dormitories';

                       ^

C:\Users\Chrysostomos\Desktop\Problem_with_char\Problem_with_char.ino:188:23: warning: overflow in implicit constant conversion [-Woverflow]

             locat[255]='Roundabout';

                       ^

C:\Users\Chrysostomos\Desktop\Problem_with_char\Problem_with_char.ino:201:23: warning: overflow in implicit constant conversion [-Woverflow]

             locat[255]='Electrical Engineers';

                       ^

C:\Users\Chrysostomos\Desktop\Problem_with_char\Problem_with_char.ino:214:23: warning: overflow in implicit constant conversion [-Woverflow]

             locat[255]='Entry of the University';

                       ^

C:\Users\Chrysostomos\Desktop\Problem_with_char\Problem_with_char.ino:227:23: warning: overflow in implicit constant conversion [-Woverflow]

             locat[255]='Aspida';

I am sorry i didn't know, thanks for your concern

Your code (except for the mistake with single and double quotes) mixes the definition and the assignment.

loc is an array of characters; when you initialise it, use double quotes. This only works when you define the array.

char loc[255] = "hello world";

If you want to assign a value afterwards, you can't use the equal sign (it's not a String (capital S)). You need strcpy().

strcpy(loc, "Hello world");

PS
strncpy is safer.

Usually, when you see something like:

if (StDir==1)
   // ...missing code...
else if (StDir==2)
   // ...missing code...
 
else if (StDir==3)
      {
   // ...missing code...

else if (StDir==4)
         }

where code blocks are controlled by a variable (e.g., StDir), it's usually more efficient and easier to read a switch/case code block:

switch (StDir) {
   case 1:          
                      // your code here when StDir == 1
      break;

   case 2: 
                      // your code here when StDir == 2
      break;

   case 3:
                      // your code here when StDir == 3
      break;

   case 4:
                      // your code here when StDir == 1
      break;

   default:     
                      // a catch-all when StDir is some other value
      break;

I think most people find the switch/case easier to read plus it is more efficient in most cases. For example, suppose you use a cascading if/else block like you have, but with 31 if blocks (i.e., perhaps one for each day of the month). On average, the code will perform 15 failed if tests each month. With a switch/case, the compiler constructs a jump table for the 31 blocks. The result is a single comparison (on StDir) and then a jump (relative) to the appropriate code block. Easier to read, more efficient code...win-win.