changing const char * with button presses

Hi,
I am building an OSC music controller using the CNMAT osc library.

I am trying to change or point to a different const char * array with different button presses, namely the _address in line 36 here:

So using that library, I have a function that runs when I press a button that looks like this:

void noteOn(char *FSR_OSC_Msg, int OSC_Velocity){

  OSCMessage msg(FSR_OSC_Msg);
  msg.add(1);
  msg.add(OSC_Velocity);
  Udp.beginPacket(outIp, outPort);
  msg.send(Udp);
  Udp.endPacket();
  msg.empty();

}

The FSR_OSC_Msg array is what I’m trying to control.
Each button sends osc messages with addresses like “/1/1”, “/1/2”, “/1/3”, etc…
But I want to press a different set of buttons , and the leading 1 changes to 2, like : “/2/1”, “/2/2”, “/2/3”, etc…

This is so I can change what messages the first set of buttons send, with a different set of buttons.

I tried creating a char array for the first part of the address:

char OSC_Msg_Prefix[32] = "/1/";

And then doing this to copy that and use the index of the buttons I’m looping through, to make an address that looks like /1/1 for group 1/button 1 for example:

//new array for new address
 char FSR_OSC_Msg[32];

//get array length
  size_t destination_size = sizeof (OSC_Msg_Prefix);

//set prefix to new address
  strncpy(OSC_Msg_Prefix, FSR_OSC_Msg, 32);

//null terminate string
  OSC_Msg_Prefix[destination_size -1] = '\0';

//new array for the fsr button number
  char FSR_number[32];

//change index int to char
  sprintf(FSR_number, "%d", index);

//concat prefix and button number into final address
  strcat(FSR_OSC_Msg, FSR_number);

I was then hoping to use

strncpy(OSC_Msg_Prefix,"/2/",32);

for example, on another button press, to change the address.

When i Serial.write() the address, I get stuff like this:
ÐÚÿ?357357151

Can anyone please help me out?
Is there a better way to do this with pointers?
Basically I just want to be able to control that address string. And I would like for the button number to come from the index number of the button.

Sorry for the broken up code, but the real code is really long and includes lots of libraries. Here’s a more full picture that I tried to simplify, stripping out how the actual buttons work, etc…:

//Open Sound Control (OSC) library for the ESP8266
#include <WiFiUdp.h>
#include <OSCMessage.h>

char ssid[] = "ssid";          // your network SSID (name)
char pass[] = "password"; 

WiFiUDP Udp;                                // A UDP instance to let us send and receive packets over UDP
const IPAddress outIp(192, 168, 0, 1);        // remote IP of your computer
const unsigned int outPort = 8001;          // remote port to receive OSC
const unsigned int localPort = 9000;        // local port to listen for OSC packets (actually not used for sending)


char OSC_Msg_Prefix[32] = "/1/";

int threshold = 20;
int number_of_FSR =4;

void setup() {
  // put your setup code here, to run once:

}

void loop() {

 //Loop through each pin and call the FSR handler
  for (int i = 0; i<number_of_FSR; i++)
  {
    handleFSR(i);
  }
}
void handleFSR(int index)
{
  //read FSR value
  int val = analogRead(index);
 
  char FSR_OSC_Msg[32];
  size_t destination_size = sizeof (OSC_Msg_Prefix);
  strncpy(OSC_Msg_Prefix, FSR_OSC_Msg, 32);
  OSC_Msg_Prefix[destination_size -1] = '\0';

  char FSR_number[32];
  sprintf(FSR_number, "%d", index);
  
  strcat(FSR_OSC_Msg, FSR_number);
  
  int last_value = 0;

  //===========Send a NOTE ON message if the given pad is currently off
  if (val>threshold)
  {

    //send a single note on for value greater than 0
    noteOn(FSR_OSC_Msg, val);
  }

  //===========Send a NOTE OFF message if the given pad is currently on
  if (val == 0)
  {
    //send a single note off for velocity of 0
    noteOff(FSR_OSC_Msg);

  }
}

void noteOn(char *FSR_OSC_Msg, int OSC_Velocity){

  OSCMessage msg(FSR_OSC_Msg);
  msg.add(1);
  msg.add(OSC_Velocity);
  Udp.beginPacket(outIp, outPort);
  msg.send(Udp);
  Udp.endPacket();
  msg.empty();

}
void noteOff(char *FSR_OSC_Msg){

  OSCMessage msg(FSR_OSC_Msg);
  msg.add(0);
  msg.add(0);
  Udp.beginPacket(outIp, outPort);
  msg.send(Udp);
  Udp.endPacket();
  msg.empty();

}

The below (excerpt from your code) copies the rubbish that is in FSR_OSC_Msg (a non-initialised array) to OSC_Msg_Prefix

  char FSR_OSC_Msg[32];
  size_t destination_size = sizeof (OSC_Msg_Prefix);
  strncpy(OSC_Msg_Prefix, FSR_OSC_Msg, 32);
  OSC_Msg_Prefix[destination_size - 1] = '\0';

Do you have source and destination swapped?

wow! thanks @sterretje. Yes I had the destination and source swapped, and even more helpful was your mention of the non-initialized array. Thats where the rubbish was coming from. I initialized it and swapped the src and destination, and its working now. Thank you!

char OSC_Msg_Prefix[32] = "/1/";

char FSR_OSC_Msg[32] = "/1/";
size_t destination_size = sizeof (FSR_OSC_Msg);
strncpy(FSR_OSC_Msg, OSC_Msg_Prefix, 32);
FSR_OSC_Msg[destination_size - 1] = '\0';

char FSR_number[32]="1";

sprintf(FSR_number, "%d", index);
  
strcat(FSR_OSC_Msg, FSR_number);

Im now getting a string like "/1/1", "/1/2", etc... and can change the prefix 1 by doing

size_t destination_size = sizeof (OSC_Msg_Prefix);
strncpy(OSC_Msg_Prefix,"/4/",32);
OSC_Msg_Prefix[destination_size -1] = '\0';

and my FSR_OSC_Msg string will = "/4/1", etc...

Thank you!

This is probably not the best way to do what I want, since im trying to build a real time music controller.

Does anyone have any advice on managing this better?

Would changing a char array and pointing to that be more memory efficient than all these string operations? Am I creating garbage by doing this?