Go Down

Topic: Arduino004 and C string functions (Read 1 time) previous topic - next topic

mrbbp

Hello, i've got some prob with the C++ string functions

my code worked well in arduino003 now, i've got an error

Quote
error invalid conversion from 'const char*' to 'char'


i think i understand what it mean but i don't know how to resolve the prob.
is it about my string copy ?
and how to modify the char type

Code: [Select]
// init poeme
int jour = 0;
int nJour = 17;
int mois = 2;
int an = 2;
char chaine[65] = " ";
char ligne[0x10] = "";
int jMax[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
const char *sJour[33] = {"", " premier", " deux", " trois", " quatre", " cinq", " six", " sept", " huit", " neuf", " dix", " onze", " douze", " treize", " quatorze", " quinze", " seize", " dix-sept", " dix-huit", " dix-neuf", " vingt", " vingt-et-un", " vingt-deux", " vingt-trois", " vingt-quatre", " vingt-cinq", " vingt-six", " vingt-sept", " vingt-huit", " vingt-neuf", " trente", " trente-et-un", " mille"};
const char *sMois[13] = {"", " janvier", " fevrier", " mars", " avril", " mai", " juin", " juillet", " aout", " septembre", " octobre", " novembre", " decembre"};
const char *sAnnee[8] = {"", ""," deux mille deux", " deux mille trois", " deux mille quatre", " deux mille cinq", " deux mille six"};
const char *sNomJour[7] = {"dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"};


void setup (void) {
 Serial.begin(115200);
 delay(100);                
}
void loop (void) {
 int i = 0;
 int j = 0;

 if (jour > 6) {jour = 0;}
 if (nJour > jMax[mois]) {nJour = 1; mois++;}
 if (mois == 2 && an == 3) { jMax[2] = 29;} else { jMax[2] = 28;}
 if (mois > 12) {mois = 1; an++;}

 // concatenation des chaines
 *chaine = " ";
 strcpy(chaine, sNomJour[jour]); // dimanche
 strcat(chaine, sJour[nJour]); // dix-sept
 strcat(chaine, sMois[mois]); // Février
 strcat(chaine, sJour[2]); // deux
 strcat(chaine, sJour[32]);// mille
 strcat(chaine, sJour[an]); // deux
 Serial.print(chaine);


 jour++;
 nJour++;
 // ecriture des lignes
 strncpy(ligne,chaine,16);
 if (strlen(ligne) >= 16) {
   ligne[16] = '\0';
 }
 // ecrit la ligne
 Serial.print(ligne);
 delay(100);
 *ligne=" ";
 for (i=2; i<=4;i++) {
   strncpy(ligne,&chaine[16*(i-1)],16);
   if (strlen(ligne) >= 16) {
     ligne[16] = '\0';
   }
   // ecrit la ligne
   Serial.print(ligne);
   delay(100);
   *ligne=" ";
 }
}


i was just trying to understand a bit C, now it is C++ :'(

any solution or a begin of solution ?

regards

eric

mrbbp

#1
Apr 30, 2006, 05:39 pm Last Edit: Apr 30, 2006, 05:40 pm by mrbbp Reason: 1
Code: [Select]
char *chaine = " ";
char *ligne = "";


i've just changed these two line and take off any *chaine = " " or *ligne=" " in  the loop()...

but pb compilation on serial...

eric

admin

what do you mean with "but pb compilation on serial..." ?

massimo

mrbbp

#3
Apr 30, 2006, 08:04 pm Last Edit: Apr 30, 2006, 09:16 pm by mrbbp Reason: 1
i mean this :
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1146411379

but now i haven't got any prob with serial error... :-X

eric

mrbbp

#4
Apr 30, 2006, 09:13 pm Last Edit: Apr 30, 2006, 09:15 pm by mrbbp Reason: 1
I've tried this code

Code: [Select]
int ledPin = 13;
// init poeme
int jour = 0;
int nJour = 17;
int mois = 2;
int an = 2;
char *chaine = " ";
char *ligne = " ";
int jMax[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
char *sJour[33] = {"", " premier", " deux", " trois", " quatre", " cinq", " six", " sept", " huit", " neuf", " dix", " onze", " douze", " treize", " quatorze", " quinze", " seize", " dix-sept", " dix-huit", " dix-neuf", " vingt", " vingt-et-un", " vingt-deux", " vingt-trois", " vingt-quatre", " vingt-cinq", " vingt-six", " vingt-sept", " vingt-huit", " vingt-neuf", " trente", " trente-et-un", " mille"};
char *sMois[13] = {"", " janvier", " fevrier", " mars", " avril", " mai", " juin", " juillet", " aout", " septembre", " octobre", " novembre", " decembre"};
char *sAnnee[8] = {"", ""," deux mille deux", " deux mille trois", " deux mille quatre", " deux mille cinq", " deux mille six"};
char *sNomJour[7] = {"dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"};


void setup (void) {
 Serial.begin(115200);
 pinMode(ledPin, OUTPUT);                 
}
void loop (void) {
 int i = 0;
 int j = 0;

 if (jour > 6) {jour = 0;}
 if (nJour > jMax[mois]) {nJour = 1; mois++;}
 if (mois == 2 && an == 3) { jMax[2] = 29;} else { jMax[2] = 28;}
 if (mois > 12) {mois = 1; an++;}

 // concatenation des chaines
 //*chaine = " ";
 strcpy(chaine, sNomJour[jour]); // dimanche
 strcat(chaine, sJour[nJour]); // dix-sept
 strcat(chaine, sMois[mois]); // Février
 strcat(chaine, sJour[2]); // deux
 strcat(chaine, sJour[32]);// mille
 strcat(chaine, sJour[an]); // deux
 Serial.print(chaine);
 Serial.println();


 jour++;
 nJour++;
 // ecriture des lignes
 strncpy(ligne,chaine,16);
 if (strlen(ligne) >= 16) {
   ligne[16] = '\0';
 }
 // ecrit la ligne
 //Serial.print(ligne);
 Serial.print(ligne);
 Serial.println();
 delay(100);
 //*ligne=" ";
 for (i=2; i<=4;i++) {
   strncpy(ligne,&chaine[16*(i-1)],16);
   if (strlen(ligne) >= 16) {
     ligne[16] = '\0';
   }
   // ecrit la ligne
   //Serial.print(ligne);
   Serial.print(ligne);
   Serial.println();
   delay(100);
   //*ligne=" ";
 }
 digitalWrite(ledPin, HIGH);   // sets the LED on
 delay(1000);                  // waits for a second
 digitalWrite(ledPin, LOW);    // sets the LED off
 delay(1000);
}


no compilation error
done uploading
but nothing appear on the serial monitor window and no blinking led

what's wrong ?

regards

eric

mellis

#5
Apr 30, 2006, 10:35 pm Last Edit: Apr 30, 2006, 10:38 pm by mellis Reason: 1
First, you have the Serial Monitor Baud Rate set to 115200, right?

Also, you're not allocating any room for chaine and ligne, so when you strcpy into them, you're overwriting random bits of RAM, which tends to cause problems.  Try something like:

Code: [Select]

char chaine[100];
char ligne[100];


or, to allocate space and initlialize the strings:

Code: [Select]

char chaine[100] = { 0 };
char ligne[100] = { 0 };


or don't bother with strcpy or strcat at all and just print the pieces one at a time.

mrbbp

#6
Apr 30, 2006, 11:12 pm Last Edit: Apr 30, 2006, 11:17 pm by mrbbp Reason: 1
thanks for the tips mellis, but that doesn't solve my prob.

how can i "reset" my string in loop()?

if i write :
*ligne=" ";
i've got this error :
invalid conversion from 'const char*' to 'char'
"ligne" is not a constant char*...
does it mean that the strcpy is only a copy of pointers and not a "physical" copy of char's array ?
i'm a bit confused!

if i compile with arduino003, that's works.
but not with arduino004...

eric

mellis

The strcpy() will "reset" the string.  I.e.

Code: [Select]

char foo[100];

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

void loop()
{
 strcpy(foo, "hello");
 Serial.println(foo);
 strcpy(foo, "world");
 Serial.println(foo);
}


will send:
hello
world

mellis

P.S.  The problem with
Code: [Select]
*ligne=" "; is that *ligne is a character (the same as ligne[0]), but " " is a string.  To create a character constant, use single quotes: 'a'.  Thus, to assign to a particular character of the string:
Code: [Select]
ligne[3] = 'b'; or
Code: [Select]
*ligne = 'b'; or even
Code: [Select]
*(ligne + 3) = 'b';  To "reset" the string in this way, you want to assign the character with ASCII code zero (not the character '0' which has ASCII code 48) to the first character in the string:
Code: [Select]
*ligne = '\0'; or
Code: [Select]
*ligne = 0;

mrbbp

:-* yesssss

i think, i've finally understood.
many thanks.

eric

Go Up