Arduino004 and C string functions

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

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

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

// 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++ :cry:

any solution or a begin of solution ?

regards

eric

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

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

massimo

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

I've tried this code

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

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:

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

or, to allocate space and initlialize the strings:

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.

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

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

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

P.S. The problem with *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: ligne[3] = 'b'; or *ligne = 'b'; or even *(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: *ligne = '\0'; or *ligne = 0;

:-* yesssss

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

eric