using char array for strings with unknown length

ide is 1.02
i am trying to figure out how to create a char array with an unknown text size

here some code, this first part of code gets executed during my Void loop when there is a card fully inserted into a mag stripe reader, this works as written. I want to add a name after welcome and of course the name is an unknown length, the name using char Z[] array( i know z doesnt reflect name, but z is the array i am using for all recieved data and at this point it happens to a name) is retrieved further down the loop in my card reader routine and that section is working i can lcd.print the name to the lcd.

i would like to add the name to the char welcome[] array but unsure how to join the text "Welcome " to z and have it in welcome[]

char welcome[] = "Welcome ";
    
   // strcopy(welcome2,welcome);
   // strcat(welcome2,name);
    Mlength=sizeof(welcome)-1;
    Dlength=16;
    if (delayMilliSeconds(1,200))
    {
      lcd.setCursor(0, 0);
      for (byte CC = 0; CC < (Dlength-pass);CC++)
      {
        lcd.print(" ");
      }
      for (byte CC = max(0,min(Mlength,pass-Dlength)); CC < min(Mlength,pass);CC++)
      {
        lcd.print(welcome[CC]);
      }
      for (byte DD = 0; DD < (Dlength-(min(Mlength,pass)- max(0,pass-Dlength)+ max(0,Dlength-pass) ));DD++)
      {
        lcd.print(" ");
      }
      pass = pass++;
      if (pass >Mlength + Dlength)
      {
        pass = 1; 
      }
      Messagetimer = micros();
    }

i am trying to figure out how to create a char array with an unknown text size

You can't do that.

I want to add a name after welcome and of course the name is an unknown length

You'll need to determine a maximum length that you want to support. I can't imagine that you want to welcome
SomeGoofballWithARidiculouslyLongNameThatMeansAbsolutelyNothing to your program.

You would have problems fitting that on the LCD anyway.

char * array is a pointer to char data in memory. You could use malloc to allocate memory for a dynamic array, but really, memory management is a lot easier if you just used a fixed sized buffer with maximum length. Strings would then be null-terminated within that buffer anyways. Using malloc or one of the variations, you'd still need to know the final buffer length to allocate at least as much memory... and then remember to free the memory. Same with char* array = new char[50], which is roughly the same thing as char array[50], except that you need to delete [] array in the end in the first case. You see, you still have to know the length to allocate enough memory for the string. Just use the fixed array with null terminated strings..

PaulS:

i am trying to figure out how to create a char array with an unknown text size

You can't do that.

I want to add a name after welcome and of course the name is an unknown length

You'll need to determine a maximum length that you want to support. I can't imagine that you want to welcome
SomeGoofballWithARidiculouslyLongNameThatMeansAbsolutelyNothing to your program.

You would have problems fitting that on the LCD anyway.

with the routine i posted there really is not a limit to the size of the name since it is scrolling

i did however figure a way to do it (somewhat)

when i get the name in the other section i give the size to Zsize. then i can create the array with a varible
so far its working with the nsert of 10 differnt cards with different size lengths of the name

char welcome[(9 + Zsize)];
    strcpy(welcome,"Welcome ");
    strcat(welcome, name);
    Mlength=sizeof(welcome)-1;

i did however figure a way to do it (somewhat)

There is, you know, no reason to do that.

You can print the "Welcome, " string with one call to lcd.print() and the name with another call to lcd.print(). Combining the two strings into one, to make one call to lcd.print() wastes a lot of resources, and accomplishes nothing.

PaulS:

i did however figure a way to do it (somewhat)

There is, you know, no reason to do that.

You can print the "Welcome, " string with one call to lcd.print() and the name with another call to lcd.print(). Combining the two strings into one, to make one call to lcd.print() wastes a lot of resources, and accomplishes nothing.

there is no where in that example sketch i posted that i even do a lcd.print(welcome)

just curious then have you even looked at the rest of the sketch i posted. it scrolls the message accross the LCD from right to left, message can be longer than the lcd, and i print both messages one after the other then part of the message will roll over to second line of screen so i have to make sure i only send a total number of characters that do not exceed the lcd display length.
also Dlength can be changed to make it work on longer displays

if the message is long then there are point at which i am only lcd.print ing the middle of the message ie chars 2-18, if i am sending to strings to the routine would that not require more resources

i cannot use the built in scrolling routines of the lcd since the built in routines scroll both lines at the same time and i do not wish for both lines to scroll

I am still open to suggestions to imrove the above routine but saying there is no reason to do something and not provideing that solution for instance actually showing a modified sketch or a line to replace or the correct code to replace it with does us no good. especially when the suggestion work what so ever otherwise why even ask for suggestions.

here is the working part of the sketch

when this runs the following will be the unkowns in the sketch
cardallin =0;
Zsize = 6;
name = "Robert"

if (cardallin == 0)
  {
    strcpy(name, "Insert Card");
    Zsize = 11;
  }
  char welcome[(9 + Zsize)];
  strcpy(welcome,"Welcome ");
  strcat(welcome, name);
  Mlength=sizeof(welcome)-1;
  Dlength=16;
  if (delayMilliSeconds(1,200))
  {
    lcd.setCursor(0, 0);
    for (byte CC = 0; CC < (Dlength-pass);CC++)
    {
      lcd.print(" ");
    }
    for (byte CC = max(0,min(Mlength,pass-Dlength)); CC < min(Mlength,pass);CC++)
    {
      lcd.print(welcome[CC]);
    }
    for (byte DD = 0; DD < (Dlength-(min(Mlength,pass)- max(0,pass-Dlength)+ max(0,Dlength-pass) ));DD++)
    {
      lcd.print(" ");
    }
    pass = pass++;
    if (pass >Mlength + Dlength)
    {
      pass = 1; 
    }
  }

The original message was deleted as it used more memory.

If your project and board allow for the memory required my second thought is something along the line of this untested function.

void display_message(const char* pszMessage, const char* pszName)
{
    int     cch = strlen(pszMessage) + 1 + strlen(pszName);
    char    buff[(3 * cch) + 1];

    char*   ptr = buff;
    memset(ptr, '\0', sizeof(buff));

    ptr    += cch;
    strcpy(ptr, pszMessage);

    ptr    += strlen(pszMessage);
    *ptr++  = ' ';

    strcpy(ptr, pszName);

    for ( int o_buff = 0; o_buff < (2 * cch); o_buff++ )
    {
        for ( int n = 0; n < cch; n++ )
        {
            lcd.print(buff[o_buff + n]);
        }
    }
}

A very good article on arduino and strings. It helped me a lot.

Man....this article is priceless!!! Created account just to post a thank you! I usually just stumbled through my sketches based on my programming experience from other languages....but this article does a great job of declaring the difference easily and straight-forward =)

Glad I put the quick time into reading this it really helped me out