Pointer syntax issue always been a pain for me

this is what I have:

main string buffer w/ max size of 150 char in ram
a bunch of small strings in PROGMEM
a lookup table of pointers to those strings in PROGMEM

what I want to do is use the strings in PROGMEM to build the main buffer without over running it.

The issue I'm having is getting the length of a particular string in PROGMEM
I have no trouble getting the right string from the lookup table but I'm unable to get the length of the string. Pointers have always given me a headache. I understand they're are addresses to where the data is stored. Its the way they are access has always been a pain for me. :frowning:

I have sizeof (string_lookuptable[i]) which is the address of where the string is stored . what i need though is the length of the actual string that is being pointed to. I get confused with the redirection * and &. format.

So how should the syntax look like to get the length of the actual string?

Post the code you have and we can help.

strlen_P ?

1 Like

Yes.

No need for pointer. Use ,strlen_P(variable name).

#include <string.h>

const char testA[] PROGMEM = "string A";
const char testB[] PROGMEM = "string B follows";


void setup() {
  Serial.begin(115200);
  delay(300);
  Serial.print("testA length = ");
  Serial.println(strlen_P(testA));
  Serial.print("testB length = ");
  Serial.println(strlen_P(testB));
}

void loop() {
  // put your main code here, to run repeatedly:
}

Explore AVR Libc.

We don't know which Arduino or "compatible" board you are using. That should have been in the first line of your post.

`#define BUFF_SIZE 150

const char string_0[] PROGMEM = "HELLO";   // "HELLO" etc are strings to store - change to suit.
const char string_1[] PROGMEM = "This space available for advertising";
const char string_2[] PROGMEM = "Single male seeks single female ) for LTR";
const char string_3[] PROGMEM = "Don't stay a stranger my Lady! ";
const char string_4[] PROGMEM = "Bye Bye";
const char string_5[] PROGMEM = "Hi!";
const char string_6[] PROGMEM = "Thank You";
const char string_7[] PROGMEM = "What do you think of my message board? ";
const char string_8[] PROGMEM = "Did you base your career choice on Star Trek ?";
const char string_9[] PROGMEM = "Are you a fan of ";
const char string_10[] PROGMEM = "Dr. Who, Stargate SG-1, Stargate Alantis, Bybalon 5";


// Then set up a table to refer to your strings.
const char * const string_table[] PROGMEM = {string_0, string_1, string_2, string_3, string_4, string_5,
                                             string_6, string_7, string_8, string_9, string_10};

char Message[BUF_SIZE] = "Hello world! how are you? ";


void assemblemess()
        {
            if( sizeof(Message) + sizeof(&string_table[j])< BUF_SIZE-1)
              {              
                addProgStr( (const char *) pgm_read_word (&string_table[j]) );
              }
            else
              storedphrase = false; // set overrun flag  
        }
        
void addProgStr (const char * str)
       {
         if (!str) 
            return;
         while ((ch = pgm_read_byte(str++)))
              Message[i++] = ch;
         Message[i] = '\0';
       } // end of printProgStr   
 `Use code tags to format code for the forum` 


Here is some of the code
I use the if statement to check to see if there is enough room for the string I want to add.

As has been pointed out, you want to use strlen, not sizeof. sizeof gives you the size of the array (in bytes), not the length of the string contained in the array.
Also be careful, string_table is stored in PROGMEM, so you need to properly access it.

void assemblemess()
{
  if ( strlen(Message) + strlen_P(pgm_read_ptr(&string_table[j])) < BUF_SIZE - 1)
  {
    addProgStr( (const char *) pgm_read_ptr(&string_table[j]) );
  }
  else
    storedphrase = false; // set overrun flag
}
1 Like

thank you :slight_smile:

That has always been my weakness .
how to properly access indirect pointers.....

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.