Am I using pointers correctly?

Im trying to write a function that updates a char string. Im guessing pointers are the way to do it. Below is what i have so far and it works but I dont know if its working correctly. And the internet where i am is to slow to open the docs site and build site

Basicaly its like a CLI on the Serial port where I enter usernames and welcome messages etc. there are 5 or 6 char arrays lengths all fixed or preallocated with [] in the struct that i store on a SD card. this is one set:

struct Username {
        uint8_t Usernumber;
        char Firstname[20];
        char Surname[20];
        char Welcome[100];
}
Username;

Here is the funtion:

void readString(char *ptr, uint8_t length)
{
    char inChar;
    int pos = 0;
    
    while (!Serial.available()); //wait for serial data to come in 
    
        
    while (Serial.available()){
        if (pos < length){
            inChar = Serial.read();
            ptr[pos] = inChar;
            pos++;
            ptr[pos] = 0;
            if (inChar == 0x0A || inChar == 0x0D){
                pos = length;
            }
        } else {
            while (Serial.available()) {Serial.read();}//throw it away
        }
    }
    return;
}

and i call the function with something like this:

Serial.print("Enter Users first name 20 char max: ");
readString(Username.Firstname, 20);
Serial.println(Username.Firstname);

This may or may not be useful to you - a recent discussion on pointers:

This post onwards : https://community.spark.io/t/sprint-7-spark-publish-released-lets-build-a-cloud-connected-motion-detector/3391/56?u=rehaan

cya
R

Thats along the lines of a post where i worked this out from… im just not sure if it is 100% correct

And another thing… i don think its possible but would love for it to be possible… a way to check the amount of space available for the string to slot into, sizeof returns the size of the pointer ie 4. and strlen() will return the length of the current string in there… I was thinking of defining the number of characters and creating the struct using that ie

#define firstnamesize 20

char Firstname[firstnamesize];

but i would still have to pass that to the function… less chance of typo i guess but also a tad more confusing.

When pos==19 then ptr[pos] is the 20th element of pos. But then you increment pos and set the 21st element to (char)0, overwriting the next memory location, the first char of Surname. Also you return the CR or the LF at the end of your strings, likely not what you want. It all can be simplified further than this:

void readString(char *ptr, int length)
{
    char inChar;
    int pos = 0;

    while (!Serial_available()); //wait for serial data to come in 

    while (Serial.available()) {
        inChar = Serial.read();
        if (inChar == 0x0A || inChar == 0x0D)
            break;
        ptr[pos] = inChar;
        pos++;
        if (pos >= length-1)
            break;
    }
    ptr[pos] = '\0';

    while (Serial.available())
        (void)Serial.read(); //throw it away

    return;
}
2 Likes

Awesome nicely spotted… Thanks very much for that

Ill update the function and give it a test… but i need to install and setup netbeans beacuse i cant get the build IDE to load on the slow internet :frowning: only 6hrs of download left to go for a 60MB file then i need to get the repositories to do the build too

1 Like