Assigning a value to a char Array (sprintf)?

Hi,

I am working on a program and I think i am have issues with memory management, partially because I am using strings (which i've been told is not great). I am doing the work to switch over to char arrays. I would post the whole code but its well over 10k lines and my question is simple *even if its XY

char * getProgramName() {
  static char programName[17];
  
  switch (ledArray[program]) {
    case 1:
      {
        
        programName = "Custom";
      }
    case 2:
      {
        programName = "Full White";
      }
    case 3:
      {
        programName = "Warm White";
      }
   // a lot of cases later
    case 87:
      {
        programName = "Christmas Slide";
      }
    case 88:
      {
        programName = "Christmas Fade";
      }
    default:
      {
        programName = "Error";
      }
  }
  return programName;
}

obviously this code does not compile because you cannot assign a char array a value. SO i did some googling and AWOL told someone to use sprintf (but there waint an example) so i looked up sprintf but I cant make sense of it...

http://www.cplusplus.com/reference/cstdio/sprintf/

Why are there a whole lot of % signs? Thanks a head of time.

I don't think sprintf is what you need, because the documentation says that it is used for formatting strings as if they were formatted using printf(). That's probably why there are a lot of % signs - they're used for formatting. You could probably use sprintf() for this task, but this might be more straightforward:

case 1:
{

String progN = "custom";
progN.toCharArray(programName, 17);
}

...and so on for the rest of your switch block.

https://www.arduino.cc/en/Reference/StringToCharArray

i suggest writing an optimized function for your program , and using it every time you need that assignment .

something like :

//declaration

void fill (char* arr, char input[]) {

 unsigned char i=0;  
 while(input[i]) *(arr+i)=input[i++];  
 *(arr+i)='\0';

                                            }

//usage

char array[99];
fill(array,"ape");

Hmm. I dont think that will work, Amine. I want it to return a phrase based on a variable.... i guess i could use fill(array, "ape") inside each case statement....

@tony, i am trying to avoid using Strings... i could just set up the function as a String and return the characters (which i how i was doing it originally)

Thanks for the replies though!

Qdeathstar:
sprintf - C++ Reference

Why are there a whole lot of % signs? Thanks a head of time.

From that page under Parameters (near the top):

str
Pointer to a buffer where the resulting C-string is stored.
The buffer should be large enough to contain the resulting string.

format
C string that contains a format string that follows the same specifications as format in printf (see printf for details).

... (additional arguments)
Depending on the format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace a format specifier in the format string (or a pointer to a storage location, for n).
There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function.

So for the format string explanation, it says to see the page on printf.

If you go to the page on printf, it explains exactly what those percent signs are there for and how they work. It's the very first main thing on the page.

It often isn't enough to just look up a function, normally you have to actually read what it says and sometimes even chase down information that it points you to.

Delta_G:
From that page under Parameters (near the top):

So for the format string explanation, it says to see the page on printf.

If you go to the page on printf, it explains exactly what those percent signs are there for and how they work. It's the very first main thing on the page.

It often isn't enough to just look up a function, normally you have to actually read what it says and sometimes even chase down information that it points you to.

Thanks for your help, Delta. I do read, no need to be insulting about it. Reading and comprehending are two different things. I appreciate your free time. Honestly.

But, you should understand, that if you saw something written in a language you dont understand it can be overwhelming to comprehend everything... This is a hobby for me, not a class project or a commercial venture.

A % followed by another % character will write a single % to the stream.

It looks like % is followed by a different character that lets the compiler know what format to display a variable in. But, it still doesn't help with this issue. I think i figured it out by guessing, though

switch (ledArray[program]) {
case 1:
{
sprintf(programName, "Custom");
break;
}
}

seems to work. But i couldn't get that from that link...

Sure you could. Format string here is "Custom" with no format specifiers. Nothing with a percent sign to replace means nothing in that string gets replaced and you just end up with the string "Custom".

On the page for printf it says of the format string:

C string that contains the text to be written to stdout.
It can optionally contain embedded format specifiers that are replaced by the values specified in subsequent additional arguments and formatted as requested.

A format specifier follows this prototype: [see compatibility note below]
%[flags][width][.precision][length]specifier

Where the specifier character at the end is the most significant component, since it defines the type and the interpretation of its corresponding argument:

So it says that the string can contain special specifiers that will be replaced by values from additional arguments. Then it tells you exactly how those work, that it is a percent sign followed by a flag. Then there's a table explaining what each flag means. I don't see what is impossible to understand here that you would ask:

Why are there a whole lot of % signs? Thanks a head of time.

Now if you had said that you didn't understand when to use this flag or that flag or something I would follow. But if you don't know why there are % signs in there, then no, you didn't read. I'm sorry if you take that as offensive, it isn't meant to be. But it does show that you didn't read that. It doesn't take any huge amount of reading comprehension to see the bit I quoted above and not understand why there are % signs in the string. Understanding exactly how to use all of that might still be confusing, but knowing what they are doing there is answered in pretty unambiguous language.