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

  • 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/

  • Thanks For the help Drew

    I learn C but about arrays I don't know very much. You say the char *webpage is a array of pointers and how can I use it?? and if I want to write this strings in the code space how can I read then..

    please explain more about that

    Thanks very much

  • char* webpage[]
    

    is an array of pointers to char. You would access the elements as with any array in C, by supplying an integer expression between the square brackets. E.g.,

    extern void SendToBrowser (char* p);
    
    U8 i;
    
    for (i = 0; i < NumLinesInWebHeader; ++i)
        {
        SendToBrowser (webpage[i]);
        }
    

    Chapter 3 of the Keil C User's Guide discusses the memory areas in the 8051 and keywords used to access them. If you want the string constant to reside in program memory, you simply declare it with the "code" memory qualifier thus:

    char const code stringConstant[] = "my string";
    

    That is, stringConstant is an array of chars residing in code memory. Given this declaration, the compiler will know to generate instructions to access the program space of the 8051 instead of the data space for this variable.

    C programs on traditional workstation-type machines get loaded into RAM from a disk image. The disk image contains a section with values for constants and initialized variables, which are copied into RAM. Clever linkers arrange it so that the loader puts the initialized values in the right addresses for the program to access. It would be unusual (at least) to see a system that tried to dynamically access the disk to read the string constants during operation of the program.

    Embedded systems, however, generally load from some sort of ROM (often flash, these days). There has to be a copy of the string constants in the non-volatile storage for them to survive while power is off. Depending on your system, you can mimic the traditional OS process of loading an image containing these constants into RAM, in which case your string constants would be located somewhere in RAM, as well as in the ROM.

    If you have read access to your program store, then to save space you might want to simply leave the string constants in ROM and access them there, rather than copying them to RAM and having two copies. With Keil C, declaring the string in "code" space serves this purpose.

    Even if you do have read access to your ROM, you might prefer the data in RAM. Often, RAM access time is much faster than flash access time, so if performance matters more than the space, you might copy the string constants to RAM anyway. It just depends on your particular system and its needs.

  • Just a note about arrays and variables.
    In fact , in ANSI C next lines are different:

    unsigned char array[] = "test";
    unsigned char *array = "test";

    First line allocates just 5 bytes; second line allocates 5 bytes for the string + some (2/4/?) bytes for the pointer to this string.