This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

New array declaration

Hi everyone

can someone tell me how to declare a array of 20 characteres. I wanto to use this array to fill with a string and then display it in a LCD. Also I wanto to use a pointer to read or write in this array

thanks

Parents
  • If your want to read and write in an array,u can declare the array in idata or data RAM,if external RAM is valid,xdata could be used

    char idata/data/xdata str[21]="read and wirte here.";
    
    Note that,in C,a string ends with a '\0',if U want to store a 20-character string in a arrary,your will have to assign 21-character space for it.

Reply
  • If your want to read and write in an array,u can declare the array in idata or data RAM,if external RAM is valid,xdata could be used

    char idata/data/xdata str[21]="read and wirte here.";
    
    Note that,in C,a string ends with a '\0',if U want to store a 20-character string in a arrary,your will have to assign 21-character space for it.

Children
  • "to store a 20-character string in a arrary,your will have to assign 21-character space for it."

    But, as I said earlier, there is no need to explicitly specify the length (eg 21) in the definition - the compiler will do it automatically.

    In fact, unless you have a specific reason to do so, it's usually best not to specify the size - then there's no risk of miscounting, or if someone changes the string without remembering to change the size...!

  • but, if the length of the string which you declared is twenty,you don't need to specify the length,but while it is less than 20, for example

    char idata str[]="hello";
    
    however,you need to store a 20-length string later on, does's it work?
    eg.
    strcpy(str,"this a 20-char sting");
    

  • "you need to store a 20-length string later on"

    that's why I said, "...unless you have a specific reason to do so..."

    In that case, yes - you would need to explicitly state the length as 21 in the definition.

    strcpy(str,"this a 20-char sting");
    The 1st parameter to strcpy just tells it where to start copying to; see strncpy if you want to limit the maximum number of characters to copy!