0

I'm working on a project, where I wish to use pointers. I had a very quick overview of using pointers in C in collage, but I don't know how to use them.

I want that function uses input String (answer) as pointer.

Calling function in setup block

void setup() {
    sendCommand("AT+RST\r\n", 5000, DEBUG, "ready");
    sendCommand("AT+CWSAP_CUR=\"mega\",\"password123\",10,3\r\n", 4000, DEBUG, "OK");
}

String sendCommand(String command, const int timeout, boolean debug, String *answer)
{
    String response = "";

    Serial1.print(command); // send the read character to the esp8266

    long int time = millis();

    while ((time + timeout) > millis())
    {
        while (Serial1.available())
        {
            // The esp has data so display its output to the serial window 
            char c = Serial1.read(); // read the next character.
            response += c;
        }

        //Waiting for the right answer and breaking a while loop
        if (response.indexOf(*answer) > 0)
        {
            break;
        }

    }

    if (debug)
    {
        Serial.print(response);
    }

    return response;
}
  • 1. Format your code nicely. Many people simply won't bother to spend the extra time to read code if you've made it hard to read, and why should they? 2. If you don't understand well how pointers work and you want to use them as a solution to a problem, you have an XY problem that you need to fix. – cjs May 1 '17 at 0:26
  • Give this webpage a read, it fully explain how strings in c++ work, even down to the memory handling. hackingmajenkoblog.wordpress.com/2016/02/04/… – Matt May 2 '17 at 10:00
2

What you have there are not String objects, but string literals. Their type is const char *

You should avoid using String objects if at all possible. Instead use character arrays.

To get a character array out of a function it is best to create one outside your function and pass it to the function as one of the parameters. The return value of the function can then be a status value indicating if the call worked or not.

1

If you want to use the String type, have a variable String rdy="ready"; and pass &rdy (the address of the variable). Same with "OK".

I assume you want to use a pointer to avoid copying?

As you are already using C++ techniques (String is C++, not C), you might want to consider to pass rdy (not a pointer), and to write const String &answer in the sendCommand parameter list (meaning: reference to a string, which will not be modified), and omit the * when using the parameter. Passing references is quite similar to pointers: no copying involved. The additional benefit is that you may even write sendCommand("AT+RST\r\n", 5000, DEBUG, "ready"); then... the "ready" is automagically converted into a String, and passed via reference.

  • This is what most people are wanting as an answer. I came here looking for examples of what could be used for various Serial LCD wrappers, the ones where control characters need to be inserted and then a passed string decomposed into bytes for individual write(). – mckenzm Jul 3 '19 at 23:21
0

You can pass parameters to functions by reference by value or 'by pointers'.

Passing a parameter by value means that the function has a separate copy of the parameter so this is more expensive in terms of memory. It also means that any changes you make to the parameter inside the function are not reflected outside the function, they are two separate variables after all.

Passing by reference in C/C++ is done by adding a & after the parameter's type, (int& byRef). This means that you are using the same variable inside and outside of the function. If you want to prevent the function changing the value then you set the parameter to be a constant reference. const int& constByRef

There is a third method which is passing a pointer. This is very useful for parameters that are a few bytes in size, i.e. strings and structures char* ptr. When you pass a pointer it is passed by value, you can modify the pointer or the data it points to. Since a pointer is usually only the word size of the processor it is only as expensive as passing an int parameter.

When you pass a const char* cPtr you can not modify the value of the pointer, but you can modify the value of the data pointed to. This type of parameter will let you pass in string literals.

To make you code cleared always write functions to accept constant parameters. If the parameter is larger than a word pass it by reference unless its an array (including char*) in which case pass it by pointer. If in doubt it should be a constant reference to start with. Sometimes embedded constraints affect these rules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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