Self teaching - coding syntax question. *solved*

So i’m butchering some code examples in an attempt to understand C++ on Arduino’s and came across what seems to be superfluous coding that’s just confusing me.

I understand there’s several ways to do something giving identical results, perhaps that is all this is, but i’m curious and my OCD won’t let it go hence my question.

Just as a reference ( it’s a 12864 LCD module and DS1307 RTC clock ), here is the Frankenstein code of mine ( which actually works well somehow ) :

#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

U8G2_ST7920_128X64_1_8080 u8g2(U8G2_R0, 4, 5, 6, 7, 8, 9, 10, 11, 16, U8X8_PIN_NONE, 17, 15); // configures parallel display connection.

void setup(void) {
  rtc.begin(); //starts the RTC module.
  u8g2.begin(); //starts the u8g2 code.
  u8g2.setFont(u8g2_font_synchronizer_nbp_tf); // global font selection. 
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // sets the RTC module time.
}

int MyDelay=1000;
int posY=0;
int posX=10;
char Str1[]="LCD Clock Test"; //defines "LCD Clock Test" as a string. 
char* daysOfTheWeek[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void loop(void) {

DateTime now = rtc.now();
  u8g2.firstPage();
   do {
    u8g2.drawStr(posY,posX,Str1);
    
    u8g2.setCursor(0,20);
    u8g2.print(daysOfTheWeek[now.dayOfTheWeek()]);
    
    u8g2.setCursor(10,30);
    u8g2.print(now.day(),DEC); 
    u8g2.print("/");
    u8g2.print(now.month(),DEC);
    u8g2.print("/");
    u8g2.print(now.year(),DEC);

    u8g2.setCursor(10,50);
    u8g2.print(now.hour(),DEC);
    u8g2.print(":");
    u8g2.print(now.minute(),DEC);
    u8g2.print(":");
    u8g2.print(now.second(),DEC);
    
 } while ( u8g2.nextPage() );

delay (MyDelay);

 }

It’s a basic merge and modification of two example programs ( the “ds1307” from the adafruit RTC library and the basic “helloworld” from the ug82 library ), please excuse the lack of ‘proper’ formatting etc. It works great and has been a huge benefit in understanding the syntax used for C++, but i’m confused with this line of original code in the RTC library example :

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

I modified the line to be simpler for me to understand, most notably removing the [7][12] array sizing (I think ? ) and changing the “char” to “char*” to make it an arrayed string ( again I think ? )

Modified line :

char* daysOfTheWeek[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

So the question(s) I have relate to and center on the whole arrayed string vs string, what is the “string[7][12]” representing vs just leaving it up to the compiler the size the string by using the “char* string” ?

Which of the two methods is more correct and why, they both seem to have the same outcome ?

Your way "wastes" two bytes per string for the pointer - you get an array of pointers to strings that are exactly the needed length.
The fixed size array "wastes" several bytes per string because most (all!) of them are shorter than 11 characters (plus the null.). You get an 84byte array containing the names and zeros.

I like your way better. It should be more compact AND easier to compute...

westfw:
Your way "wastes" two bytes per string for the pointer - you get an array of pointers to strings that are exactly the needed length.
The fixed size array "wastes" several bytes per string because most (all!) of them are shorter than 11 characters (plus the null.). You get an 84byte array containing the names and zeros.

I like your way better. It should be more compact AND easier to compute...

Wow, firstly cheers for the rapid reply.

Now if i understand you correctly, the [7] part of the original is saying there's seven variables ( each day of the week ) and the [12] part is to reserve 12 bytes for each of the 7 variables in the string ( hence the 84 byte total ) ?

And the way iv'e done it uses a byte per letter + 2 bytes wasted for each word, resulting in only 64 bytes total used ?

Yep. Although both ways need a null at the end of the string, so it's 84 vs 71.

westfw:
Yep. Although both ways need a null at the end of the string, so it's 84 vs 71.

right, so the wasted 2 bytes don't cover the null byte, got it.

Thanks heaps, you've just saved me hours of reading lol.

This:

char* daysOfTheWeek[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

Is also better because you let the compiler figure out how many elements the array needs. Thus, you won't screw it up with something like this:

char* daysOfTheWeek[5] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

But, you should make it idiot-proof:

const char * const daysOfTheWeek[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};