Concatination char arrays, help needed

I'm not a professional coder. I'm a retired hardware designer of FPGA and ASICS. So please bear with me. My hobby is robotics and other electronic based projects.

I want to pass to a concat function char arrays A, B, and concat to char array O . The attached code works just fine.

It seems that the size of the arrays should not be needed. Is there a way to pass these arrays and determine the array size within the function?

replace this function call:
concatCharArraysABtoO(A, sizeof(A), B, sizeof(B), O, sizeof(O));
with this: concatCharArraysABtoO(A, B, O);

Another question is there an easier way to concat char arrays?

CharArrayToFunction.ino (1.1 KB)


char A[] = "DIR\\";
char B[] = "FILENAME.TXT";
char O[sizeof(A) + sizeof(B) - 1];

void setup() {
	Serial.begin(1000000);
	Serial.print("\n\n\n\n\n\n\n");
	Serial.flush();
	Serial.println("Begin\n");
}

void loop() {
	
	if (!concatCharArraysABtoO(A, sizeof(A), B, sizeof(B), O, sizeof(O))) {
		Serial.println(F("ERROR: concatCharArraysABtoO() Failed!"));
	} else {
		// print output char array
		Serial.println(O);
	}
	
	Serial.println(F("\nAll Done"));
	while (true) { delay(1000); }
}

bool concatCharArraysABtoO(char a[], byte asize, char b[], byte bsize, char out[], byte outsize) {
	
	if (asize + bsize - 1 > outsize) {
		out[0] = 'X'; // null
		out[1] = '\0'; // null
		Serial.println(F("Failed Concat: a & b char arrays larger then output array"));
		return false;
	}
	
	int i;
	// cat a to out           skip nul
	for( i = 0; i < outsize && i < asize-1; i++ ) {
		out[i] = a[i];
	}
	// cat b to out                add nul
	for( int ib = 0; i < outsize && ib < bsize; i++, ib++ ) {
		out[i] = b[ib];
	}
	return true;
}

If they're all strings, then you could use strlen.
Obviously, this won't work on an empty destination string

string functions exist to do what you're asking

char A[] = "DIR\\";
char B[] = "FILENAME.TXT";
char O[50];

void setup() {
    	Serial.begin(1000000);
    	Serial.print("\n\n\n\n\n\n\n");
    	Serial.flush();
    	Serial.println("Begin\n");

        strcpy (O, A);
        strcat (O, B);
    	Serial.println(O);
}

void loop() {
}

I'm trying to do this without the cost of string overhead.

So C code has no better way to concat char arrays then strings?

Overhead?

c strings are nothing but the arrays you defined.
are you confusing a c string with an Arduino String?

more use of program memory and data memory. This code adds 35 data bytes and 306 program bytes. If recollection serves Strings use more memory. I've a tight budget in both program and data memory.

I think I must be confusing them. I'll look into c code strings.

i've found there's plenty of flash. i've run out of data space but had plenty of flash. you can store static (const) data and strings in flash. see PROGMEM

@gcjr Thanks, I'm doing that already. I'm at 23000 Program and and not much more then 200 left over bytes for data. When it gets real tight I'll start stripping out anything not needed like error messages. I know before I'm done there will be a crunch. So crunching it down as I go is way easier.

what kind of application requires that much memory?

I cannot reveal the project until the patent is filed. I will say the data structures are significant and must be held in memory for speed. Too much data for memory of either kind. EPROM is also used, this is cumbersome, but good for startup purposes.

I'm adding an SD Card to store data that is swapped in and out during configuration selections.

char A[] = "DIR\\"
                   "FILENAME.TXT";

The compiler will see this as a single string literal.
Zero overhead

The code written is just an example of concat char arrays and a starting point. I could do this

char PATH_FILENAME = "DIR//FILENAME.TXT"; 

But I want the directory to be the same with file names appended.

can't you describe a field: optics, motors, robotics, electromagnetics, radio, web?

it's hard to believe you wouldn't need the string functions elsewhere 1538/1566 byte of text

char A[] = "DIR\\";
char B[] = "FILENAME.TXT";
#if 0
char O[50];
#else                               // 224 data
char O[sizeof(A) + sizeof(B)];
#endif

void setup() {
    	Serial.begin(9600);

#if 1                               // 1566 text
        strcpy (O, A);
        strcat (O, B);
#elif 1                             // 1538 text
        char *q;
        char *p = O;
        for (q = A; *q; p++, q++)
            *p = *q; 

        for (q = B; *q; p++, q++)
            *p = *q; 
#endif

    	Serial.println(O);
}

void loop() {
}

If we're really concerned about memory, then

char O[sizeof(A) + sizeof(B) - 1];

ought to be sufficient :smiley:

I've copied this an looking it over. Thanks.

I agree this is more efficient as long as the totality of char dose not overflow the "O" char array

char O[sizeof(A) + sizeof(B) - 1];

I looked this over and added the following options for testing. Both works and both match the same usage of resources.

Exactly what I was trying to figure out! Thanks.

Also the for loops are quite confusing. I'm not familiar enough with that syntax. I'll look it up.

#elif option2
	concatCharArraysABtoO(A, B, O);
#elif option3
	concatCharArraysABtoO(A, B, O);
#endif


#ifdef option3
bool concatCharArraysABtoO(char a[], char b[], char out[]) {
	strcpy (out, a);
	strcat (out, b);
}
#endif

#ifdef option2
bool concatCharArraysABtoO(char a[], char b[], char out[]) {
	char *q;
	char *p = out;
	for (q = a; *q; p++, q++)
		*p = *q; 
	
	for (q = b; *q; p++, q++)
		*p = *q; 
	
	return true;
}
#endif