Add char to char*

How can i add an char to an char*?

Or how can i transfer an char[] to char*;

char * ret;
char aa[3] = {'1','2','3'};

i need ret to be "123"

A pointer points to a memory location. As defined in your example, ret does not point to anything.

ret = aa; will make ret point to the same memory space that aa points to.

Keep in mind that you won't be able to use ret in any string function, though, since ret is not a string, which by definition is a NULL terminated array of characters, which is not what aa is.

if i use ret=aa;
and then Serial.print(ret);
ret is empty

i did try char aa[4] = {'1','2','3','\0'}; the result is the same

for a better understanding of my problem
char *Ret="";//global
test(989,4); have to make Ret looks like "0989"

void test(unsigned int Value, byte Len)
{
  Ret = "";Serial.print("in");
  char Tmp[Len+1]; 
  for (int i = Len-1; i >= 0; i--) 
  {
    Tmp[i] = (char)(Value % 10L);
    Value = Value / 10L;  
  }
  Tmp[Len+1] = '\0';
  Serial.print("out");
  Ret = Tmp;
  Serial.print(Ret);
}
char Tmp[Len+1];

What happens to "Tmp" at the end of "test"?
It goes out of scope.

Some questions. If you are passing in a 3 digit number, why is the 2nd argument 4?

In the loop, the initial value for i is 3, and I decrements until it is 0. So, in the loop, you are storing values in Tmp[3], Tmp[2], Tmp[1], and Tmp[0].

After the loop ends, you are storing a value in Tmp[5], which is outside the bounds of the array. Changing that to store a NULL in Tmp[4] doesn't do anything, since you have already stored a NULL in Tmp[0].

   Tmp[i] = (char)(Value % 10L);

Did you mean   Tmp[i] = (char)(Value % 10L) + '0';?

Secon argument is 4 because i want to display that number as "0123"
if i have two digits test(12,4) then it will be "0012"
this string will be passed to the drawText function of the lcd that accepts char*

Did i mean Tmp = (char)(Value % 10L) + '0';? No, all i need is to break the number in digits

No, all i need is to break the number in digits

But, the number 4 and the character '4' are not the same thing. The character '4' is '0' + 4.

And, certainly the digit '0' and the value 0, in a character array, are nowhere near the same thing.

OMG :astonished:, you are right I forgot about that
THANK YOU

Got another problem now

void test(unsigned int Value, byte Len)
{
  Ret = "";
  char Tmp[Len]; 
  for (int i = Len-1; i >= 0; i--) 
  {
    Serial.println(i);
    Tmp[i] = (char)(Value % 10L) + '0';
    Value = Value / 10L;  
  }
   Serial.println("--------------");
  for (int i = 0; i <Len; i++) 
  {
    Serial.println(Tmp[i]);
  }  
   Serial.println("--------------");
  Ret = Tmp;
  Serial.println(Ret);
  lcd.drawText(20,10,Ret,RED,YELLOW);
}

returns
3
2
1
0

0
2
3
4

0234"¿÷þÿÿÿ9ÿ÷ÿ'@u\

Did you read reply #4?

works

void test(unsigned int Value, int Len)
{
  char Tmp[Len+1];
  Tmp[Len] ='\0'; 
  for (int i = Len - 1; i >= 0; i--) 
  {
    Tmp[i] = (char)(Value % 10L) + '0';
    Value = Value / 10L;  
  }
  Ret = "";
  Ret = Tmp;
  //lcd.drawText(20,10,Ret,RED,YELLOW);
}

I wouldn't rely on it.
See reply #4

@gvi70000:
Here

void test(unsigned int Value, int Len)
{
  char Tmp[Len+1];

you have defined a character buffer on the stack.

Later, here,

 Ret = Tmp;
}

You assign the address of the buffer (on the stack remember) to a global pointer, and then return.

This is seldom a good idea, because shortly, another function could be called that could reuse that memory on the stack for its own purposes.

"Ret" would still point to the same address, but the contents may have changed.

If you ever return a pointer to something from a function, think carefully about where the pointer is pointing to.

thanks
in the mean while i discovered that i need the value in other places and to overcome the tmp going out of scope i made the following

#include <S65Display.h>
S65Display lcd;
#define BLACK		RGB(0,0,0) 
#define RED		RGB(255,0,0) 
#define YELLOW	        RGB(255,255,0) 
#define WHITE	        RGB(255,255,255)
char *RetStr;
char buf[10];
unsigned int c =0;
void setup()
{
  Serial.begin(9600);
  lcd.init(); 
  lcd.clear(BLACK);//RGB(255,0,0)
}
void loop()
{
//  byte a =0;
for(unsigned int i = 0;i<65535;i++)
{
  IntToStr(i,99999);
lcd.drawText(20,10,RetStr,RED,YELLOW);
delay(10);
RetStr="test";
lcd.drawText(20,10,RetStr,RED,YELLOW);
delay(10);
lcd.drawText(20,30,itoa(c,buf,10),RED,YELLOW);
c++;
delay(10);
}
}
void IntToStr(unsigned int Value, unsigned int Max)
{
  byte Len = 0;
  if(Max < 10)
    Len = 1;
  else if(Max < 100)
    Len = 2;
  else if(Max < 1000)
    Len = 3;
  else if(Max < 10000)
    Len = 4;
   else 
    Len = 5;   
  RetStr = (char *)malloc(sizeof(char) * (Len + 1));
  RetStr[Len] = '\0'; 
  for (int i = Len - 1; i >= 0; i--) 
  {
    RetStr[i] = (char)(Value % 10L) + '0';
    Value = Value / 10L;  
  }
}

the problem is that after 200-250 calls of the IntToStr the program stops running.
Ret is used on other function where it gets string values ("Camera", "Sensor"...)
Any clue why?

Every call to malloc needs a matching call to free. You are using up all available memory, then failing to allocate memory, then dereferencing a NULL pointer, and crashing.

If you're going to keep it global, there's little point in keeping mallocing and freeing - you may as well just declare a large buffer globally.

if i use

  free(RetStr);  
  RetStr = (char *)malloc(sizeof(char) * (Len + 1));

then i only get to 1. Maybe i don't use it the right way?

RetStr i think that have to be the right size, otherwise i will print garbage on the screen.

You're freeing it before you've used it?