Change variables in char array

Hello I found in Ethercard library following code:

    Stash::prepare(PSTR("GET http://$F/$F HTTP/1.0" "\r\n"
                        "Host: $F" "\r\n"
                        "Connection: close" "\r\n"
                        "User-Agent: Arduino" "\r\n"
                        "Content-Length: $D" "\r\n"                        
                        "\r\n"
                        "$H"),
            website, PSTR(URL_PATH), website, stash.size(),  sd);

I would like do the same with normal C char array. If I write

  char test[150] =      "Host: $F" "\r\n"
                        "Connection: close" "\r\n"
                        "User-Agent: Arduino" "\r\n"
                        "Content-Length: $D" "\r\n"                        
                        "\r\n",
            website, 150);

compilar give me an error.

How is possible modify array?

How is possible modify array?

By using proper syntax. You have declared an array that contains 150 elements, and supplied three initializers, without the curly braces but with unbalanced parentheses. None of the initializers is of the proper type.

Try the Standard C library routine strcpy(). For example, if you want to copy "Hello World" into msg, then:

char msg[12];    // Don't forget space for the null termination character '\0'

strcpy(msg, "Hello World");
Serial.println(msg);

The general format is: strcpy(destinationCharArray, sourceCharArray). There is a complete family of such functions starting with the "str*" lead-in. You should also learn about the "mem*" family, too.

Do you want to modify an existing array in the program or declare and initialise a new one ?

char website[] = {"www.google.com"};
char *  test[] = {"Host: $F", "\r\n","Connection: close", "\r\n","User-Agent: Arduino", "\r\n", "Content-Length: $D", "\r\n","\r\n", website, "150"};

First, it is NOT a good idea to use up precious SRAM with string constants. That's why the PSTR() and F() macros exist to make it easier to keep string constants in FLASH.

If .prepare() will accept string constants in SRAM you ought to be able to do it with the following syntax:

 char test[] = "Host: $F" "\r\n"
                        "Connection: close" "\r\n"
                        "User-Agent: Arduino" "\r\n"
                        "Content-Length: $D" "\r\n"                        
                        "\r\n";

    Stash::prepare(test, website, PSTR(URL_PATH), website, stash.size(),  sd);

Of course if prepare() is expecting those string constants to be in FLASH it will not work and since the compiler doesn't differentiate between addresses in the SRAM address space from addresses in the FLASH (PROGMEM) address space the compiler won't be able to tell you that you are using an SRAM address where a FLASH address is expected.

UKHeliBob:
Do you want to modify an existing array in the program or declare and initialise a new one ?

char website[] = {"www.google.com"};

char *  test[] = {"Host: $F", "\r\n",
"Connection: close",
"\r\n","User-Agent: Arduino", "\r\n",
"Content-Length: $D", "\r\n","\r\n",
website, "150"};

Thats exactly what I need!

What is $F and $D? Escape sequence for string is S. There are any other $ sequences?

martin159:
What is $F and $D? Escape sequence for string is S. There are any other $ sequences?

$F and $D are just place holders put in there by the library author, they have no special meaning to the compiler. You'll need to use something like sprintf() with the %s format specifier.

martin159:

UKHeliBob:
Do you want to modify an existing array in the program or declare and initialise a new one ?

char website[] = {"www.google.com"};

char *  test[] = {"Host: $F", "\r\n",
"Connection: close",
"\r\n","User-Agent: Arduino", "\r\n",
"Content-Length: $D", "\r\n","\r\n",
website, "150"};

Thats exactly what I need!

What is $F and $D? Escape sequence for string is S. There are any other $ sequences?

Variadic Macros (The C Preprocessor)

Moderator edit: quote tags corrected.