Go Down

Topic: Assigning char arrays (Read 3472 times) previous topic - next topic

KevinT

Hi,
as memory is critical for me I want to use limited char arrays instead of Strings, but how do I pass on such a char array?

That is what I am trying to do:
Code: [Select]

char arrTest1[10];

void assign(char arrTest2[10])
{
  arrTest1 = arrTest2;


void loop()
{
assign("Name");
// arrTest1 should  now be "Name"
}


Is there a way to do it or do I have to use Strings instead?

Kevin

bubulindo

Code: [Select]

char arrTest1[10];

void assign(char* arr)
{
  strcpy(arr, "Name");


void loop()
{
assign(arrTest1);
// arrTest1 should  now be "Name"
}

This... is a hobby.

KevinT

Thanks, that is one step in the right direction,  but I need the assign function the other way around. Giving it the "Name".

I tried to adapt your code - this should do:

Code: [Select]

char arrTest1[10];

void assign(String Test2)
{
  char chTest[10];
  Test2.toCharArray(chTest, 10);
  strcpy(arrTest1, chTest);


void setup()
{
}

void loop()
{
assign("Name");
// arrTest1 should  now be "Name"
}


Thanks,
Kevin

majenko

Code: [Select]

char arrTest1[10];

void assign(char *arrTest2)
{
  strcpy(arrTest1,arrTest2);


void loop()
{
assign("Name");
// arrTest1 should  now be "Name"
}


Or, you can pass both:

Code: [Select]

char arrTest1[10];

void assign(char *dest,char *arrTest2)
{
  strcpy(dest,arrTest2);


void loop()
{
assign(arrTest1, "Name");
// arrTest1 should  now be "Name"
}


Or you can simplify it and forget the function entirely:

Code: [Select]

strcpy(arTest1,"Hello");

Go Up