Trying to create a function that would return an array

I’m trying to create a function that will right justify some integer data. And pad the front of the data to fill the specified field width.

If passed field = 5 and data =34 it should return

sss34 Where s are spaces.

My code works when its inline and not in a function.

As a function I get compile warnings and the code will not run.

D:\Arduino_ReferenceCode\memmove\RtJustify\RtJustify.h: In function 'char* RightJustify(uint8_t, uint8_t)':
D:\Arduino_ReferenceCode\memmove\RtJustify\RtJustify.h:47:10: warning: address of local variable 'destinationField' returned [-Wreturn-local-addr]
char destinationField[] = {0x2D,0x2D,0x2D,0x2D,0x2D,0x2D,0x2D,0x2D,0x2D,0x2D}; //{0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20};

I’m not great when using pointers however it seems I would have to do one of these:

  • malloc for the array “destinationField”
  • Create “destinationField” as a static.
  • or could I just make “destinationField” a global?

Any guidance would be appreciated

Thanks

John

In the below, destinationField is filled with spaces (I’ve used dashes for debugging). More spaces than I need, then I terminate the array with \0 for the length of the printing field.
I then insert my data into this array at a position that ends up being right justified.

The array spaces insures I clear the field when going from say 143 to 95

char* RightJustify(uint8_t field, uint8_t data)
{

    char buffer[10];  // longer than we expect any value to be
    char destinationField[] = {0x2D,0x2D,0x2D,0x2D,0x2D,0x2D,0x2D,0x2D,0x2D,0x2D};
    destinationField[field] = '\0';  // truncate the destination field to the length of the field

    itoa(VOCvalue, buffer, 10);
    uint8_t valueLen = strlen(buffer);

    memmove (destinationField + (field - valueLen), buffer, valueLen);
    
    return destinationField;

}




You should / can not return a local array. The array goes out of scope at the moment that the function ends and you will have a pointer pointing to something that no longer exists; that something might or might not contain the data.

You can make destinationField a static array. Or you can pass it as an argument (created elsewhere, e.g. the calling function).
Dynamic memory allocation is dangerous if you forget to free it afterwards and keep on allocating.
And global variables is always a possibility.

  • Is this a job for snprintf() to fill a buffer ?

Could be. I don’t (haven’t) used any of the printf variants and couldn’t see how to prepend the space padding.

Thanks, I’ll try static array and if I have any trouble I’ll go to global.

Using the printf family

  1. %d just prints the number
  2. %3d prints with a minimum width of 3 digits; if the number is smaller than 3 digits it will pad with spaces
  3. %03d prints with a minimum of 3 digits; if the number is smaller than 3 digits it will pad with zeroes.

If you need space padding after the digits you can use %-3d.

I think handing the output buffer as an argument is the best way.
Just like snprintf().
(Like @sterretje suggested)

xxprintf does it for you.

Many Thanks to all of you.

I’ve changing to use the sprintf function.

It wasn't me :smiley: Honours go to @LarryD in post #3.

snprintf has the befit of preventing buffer over writing.