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

About variables and arrays

Hi everyone

I want to develop a web server using the cygnal 8051 mcu, but I want to know how can I declare a varible or array for the next data:


Data "HTTP/1.0 200 OK" , $0d , $0a
Data "Content-type: text/html" , $0d , $0a ,
Data "<html><head><title>Test </head>"

please someone who know how to declare a global variable to put in this variable, strings, constant and how is the best way to pointer to this variable too



maybe can be :

unsigned data webpage [] = " ..." is it ok??


thanks for the help

Parents
  • In C, strings are arrays of characters ("char"). If you intend to use the C library string-handling functions, they should always be null-terminated, which is to say they need one extra byte at the end with value 0 to mark the end of the string.

    Literal strings in C (surrounded by double quotes) have type pointer-to-char (char*), and include the null termination.

    You can insert arbitrary characters into a C literal string with the backslash \ escape operator. Commonly-used characters like CR and LF have special codes.

    You may or may not need to access your string constant line-by-line. Perhaps you can treat it as one long string. As one long string,

    char* webpage =
       "HTTP/1.0 200 OK\r\l"
       "Content-type: text/html\r\l"
       "<html><head><title>Test </head>";
    

    (Note that this syntax relies upon the C compiler concatenating adjacent string literals. Otherwise, you'll have to have a wide line, or use the older syntax with a backslash to continue the string on the next line.)

    If you need it line-by-line:

    char* webpage[] =
       {
       "HTTP/1.0 200 OK\r\l",
       "Content-type: text/html\r\l",
       "<html><head><title>Test </head>"
       };
    

    Note that webpage is an array of pointers, not an array of characters.

    Most likely, this variable is really a constant, and should be declared "char const* const". And you might want the Keil "code" directive to keep the string constant in code space rather than copying it into RAM.

    You should probably get a good C reference book. Here's on online reference:

    http://www-ccs.ucsd.edu/c/

Reply
  • In C, strings are arrays of characters ("char"). If you intend to use the C library string-handling functions, they should always be null-terminated, which is to say they need one extra byte at the end with value 0 to mark the end of the string.

    Literal strings in C (surrounded by double quotes) have type pointer-to-char (char*), and include the null termination.

    You can insert arbitrary characters into a C literal string with the backslash \ escape operator. Commonly-used characters like CR and LF have special codes.

    You may or may not need to access your string constant line-by-line. Perhaps you can treat it as one long string. As one long string,

    char* webpage =
       "HTTP/1.0 200 OK\r\l"
       "Content-type: text/html\r\l"
       "<html><head><title>Test </head>";
    

    (Note that this syntax relies upon the C compiler concatenating adjacent string literals. Otherwise, you'll have to have a wide line, or use the older syntax with a backslash to continue the string on the next line.)

    If you need it line-by-line:

    char* webpage[] =
       {
       "HTTP/1.0 200 OK\r\l",
       "Content-type: text/html\r\l",
       "<html><head><title>Test </head>"
       };
    

    Note that webpage is an array of pointers, not an array of characters.

    Most likely, this variable is really a constant, and should be declared "char const* const". And you might want the Keil "code" directive to keep the string constant in code space rather than copying it into RAM.

    You should probably get a good C reference book. Here's on online reference:

    http://www-ccs.ucsd.edu/c/

Children