Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am working in an interactive project in displaying texts in POV which people can click to generate different texts automatically.

I wrote a String to store the text first, but it seems cannot display it. Then I change the code to use char* to store the text, but it still does not work.

Can anybody help to solve this problem....?

char* myStrings[] = {
  "a", "c", "d", "f",
  "b", "e", "v", "B", "C"];

Error

The code:

loop(){

  //xbee
  // potVal = analogueRead(potPin);
  // xbee.println(potVal);
  for (int t=0; i<sizeof(myStrings)-1; i++) {
    displayChar(myStrings[i];
  }
  for (int i=0;i<7; i++)
    digitalWrite(leds[i],LOW);
  delay(10);

Screenshot of sketch

share|improve this question
    
You intend for myStrings to be an array of strings, or an array of c-strings (char*) , or an array of individual characters? the name suggents one thing, but displayChar(myStrings[i]); seems to suggest the other. – BrettAM Mar 26 '15 at 23:01
    
I intend to make myStrings an array of 9 char* ... store 9 characters... once arduino receive command from processing so it can display one of the character...but now it can not access to the data in myStrings ? char* myStrings [9] = { "a", "c", "d", "f", "b", "e", "v", "B", "C"}; Sorry for the poor explanation...Any help would be greatly appreciated. – Charcoal Mar 27 '15 at 6:16
    
an individual character is just a char, a c-style string of one or more characters is a char*. I'm still not certian, do you want myStrings[i] to be the i'th single character from the string "acdfbevBC", or do you want it to be the i'th of 9 unrelated strings? – BrettAM Mar 27 '15 at 16:16
    
yes...I want myStrings[i] to be the i'th single character from the string "acdfbevBC"... but now I got confused with the code and I don't know how to correct it... Really appreciated for your help btw! – Charcoal Mar 27 '15 at 16:37

Like the error message says, each entry is a char*. Convert it to an array of characters instead.

share|improve this answer

First thing, in C++ (or C) a character in single quotes is just a single lone character, but any characters in double quotes is a string literal. for example "hello" is a const char[6] containing the characters 'w','o','r','l','d','\0'. That extra '\0' is the null terminator that makes an array of chars into a c-string.

Your code is making a variable

char* myStrings[] = { "a", "c", ... };

This is an array of char*, which means each element is a char*, which in turn means each element is a full string (in your case each has 2 characters).

You want an array of single characters, which means you either drop the * in char*, or drop the [] in myStrings[], and use either an array of single characters {'a','b',...}, or a single string literal "acdfbevBC". Any of these lines will work in your code:

const char * myStrings = "acdfbevBC";
char myStrings[] = "acdfbevBC";
char myStrings[] = { 'a', 'c', 'd', ... 'C', '\0'};

If you never plan to print myStrings as a unified string, you can leave off the '\0', the null terminator is there so functions that you pass it to know when it stops without you having to explicitly pass how long the string is.

Also, the first line is const char * because you should never overwrite a string literal. char myStrings[] is fine without const because the compiler is using the literal to initialize an array that gets its own place in memory, not making myStrings point into a separate cache of string literals.

Check out cpp reference's details here for more details on string literals.

share|improve this answer
    
I think I get it! Thanks for your help!! – Charcoal Mar 28 '15 at 5:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.