配列と文字列

mbed array & string ※結果は、キャラクターLCDに出力する事を前提に

配列の中に文字列を格納して出力

const char * const foodlist[] = {"Sushi","Tenpura","Unagi","Icecream","Umeboshi"};

int main() {
 lcd.printf("I like %s",foodlist[0]);
 //Result: I like Sushi

 lcd.printf("I love %s",foodlist[4]);
 //Result: I love Umeboshi
}

配列数を取得する

const char * const foodlist[] = {"Sushi","Tenpura","Unagi","Icecream","Umeboshi"};
int foodlist_num = sizeof(foodlist)/sizeof(foodlist[0])-1;

int main() {
 lcd.printf("I have %d favorite food",foodlist_num);
 //Result: I have 5 favorite food
}


Please log in to post comments.