char pointer sum

Hi everyone,

why does this work:
char * foo = "aaa" "bbb" "ccc";
It contains aaabbbccc

but not:
char * foo2 = x "ccc"; //(where x a variable of any type either char or char * etc etc(i tried both for testing))?
or also not:
char * foo2 = x "," y; //(where x and y variables of any type either char or char * etc etc(i tried both for testing))?

what i am trying to achieve is:

on a char *foo to have keys_aray[p]->get_serial() with comma in between keys_aray[p]->get_name()

char *foo=keys_aray[p]->get_serial() "," keys_aray[p]->get_name(); //DOES NOT WORK

part of a class which on Serial.println prints what i need when calling
Serial.println(keys_aray[p]->get_name()) or Serial.println(keys_aray[p]->get_serial())

    const char *get_serial() {
      return c_serial;
    }
    const char *get_name() {
      return c_name;
    }

To concatenate (not "sum") c-strings (null-terminated char arrays), use strcat() or one of its variants.

why does this work:
char * foo = "aaa" "bbb" "ccc";

Because the compiler allows it. That does not mean it a good idea to initialize variables in that way.

chris700:
Hi everyone,
why does this work:
char * foo = "aaa" "bbb" "ccc";
It contains aaabbbccc

Because the C compiler, at the time this sketch is built into and executable image, treats this initializer as a single string. This conveniently allows you to initialise a C string to something long (for instance - a chunk of XML).

If you want a number converted to a string at run time, then some sort of function needs to figure out how to do that. eg: itoa(), or sprintf().

char *foo=keys_aray[p]->get_serial() "," keys_aray[p]->get_name(); //DOES NOT WORK

You could use a String object:

String foo = keys_aray[p]->get_serial();
foo += ",";
foo += keys_aray[p]->get_name();

This might work if you want to use a character array:

size_t serialLen = strlen(keys_aray[p]->get_serial());
size_t nameLen = strlen(keys_aray[p]->get_name());
char foo[serialLen+nameLen+2];  // Add 2 for the comma and the terminating null.
strcpy(foo, keys_aray[p]->get_serial());
strcat(foo, ",");
strcat(foo, keys_aray[p]->get_name());

separate strings is useful with long strings that you would like to put on separate lines (if you what to limit line lengths)

   char *s = "this house"
              "is blue";

gfvalvo:
To concatenate (not “sum”) c-strings (null-terminated char arrays), use strcat() or one of its variants.

sorry for that, my english sometimes aren’t the best, especially late at night :smiley:

gcjr:
separate strings is useful with long strings that you would like to put on separate lines (if you what to limit line lengths)

   char *s = "this house"

“is blue”;

thanks but that doesn’t work if I use variables :confused:

johnwasser:
This might work if you want to use a character array:

size_t serialLen = strlen(keys_aray[p]->get_serial());

size_t nameLen = strlen(keys_aray[p]->get_name());
char foo[serialLen+nameLen+2];  // Add 2 for the comma and the terminating null.
strcpy(foo, keys_aray[p]->get_serial());
strcat(foo, “,”);
strcat(foo, keys_aray[p]->get_name());

Thank you so much! That worked, I am not doing that size_t thing I was using the max (char foo[1000]) and then add /0 in the end after the last strcat but your method seems much better and less memory consuming.

I see some people also they “free” the memory of char arrays or array of objects etc when they are done with what they had to do… Does only memset frees the memory or even

 for (int d = 0; d < array_items; d++)
    delete foo_aray[d];

frees the memory? or have to do both?

Use 'delete' only when you allocate memory for the array using 'new'.

Local variables, like the 'foo' array in my example, will automatically be deleted when they go 'out of scope': when you leave the set of brackets containing the declaration.

Thanks, yes i was using delete for an array of "new" objects.
What about global scope? Can I empty-free the memory when done using them and then if reused again then allocate memory again.? If yes, can you give me can example? (I imagine memset 0 would do the job in a char array?) Thank you

chris700:
Thanks, yes i was using delete for an array of "new" objects.
What about global scope? Can I empty-free the memory when done using them and then if reused again then allocate memory again.? If yes, can you give me can example? (I imagine memset 0 would do the job in a char array?) Thank you

I believe that arrays declared globally have to have a size that is a compile-time constant.

Use a global pointer to point to dynamically allocated memory if you want to free the memory and use it again in a different dynamic allocation.