Serial.print'ing a char str[#]

I give up!

I have a string class with this function:

m_pchBuff is simply declared in my class as char m_pchBuff[MAX_LEN] where MAX_LEN = 40
In my string class I have this function:

CString& CString::operator =(const char* pchVal)
{
//strncpy(m_pchBuff, pchVal, MAX_LEN);
char str[4] = "dfg";
//strcpy(m_pchBuff, pchVal);
Serial.print("");
Serial.print(str);
Serial.print(" ");
Serial.print(MAX_LEN);
Serial.println("
");
return *this;
}

I can write to the local char array 'str' as I please but I cannot write to m_pchBuff in any way what so ever without getting garbage with a Serial.println call.

The Serial.println call on the local 'str' works just fine.

What is the difference? They are both simple char arrays!

http://snippets-r-us.com/

Please use code tags. Your posted code doesn't prove anything. You need to post complete code, that compiles, that demonstrates the problem.

could you post your whole code?

I changed it so that m_pchBuff and m_pchTempBuff are allocated on the heap with ‘new’

I still have the same problem. I can’t use strcpy or strncpy on them and nor can I use on them…at least as far as Serial.print goes.

May be it is Serial.print that is the problem and not the string manipulations.

CString.cpp (13.7 KB)

CString.h (7.12 KB)

I’d suggest that you get all that implementation crap out of the h file. It belongs in the cpp file.

Your class is worse than the standard crappy String class. Memory is allocated using new, but it is NEVER deleted, even when the object that allocated it goes out of scope.

m_pchTempBuff is never pointed to anything, and yet you try to write to it, often.

CString& CString::padLeft(char* pchBuff, char cPadWith, byte nMaxLen)
{
	CString::padLeft(m_pchBuff, cPadWith, nMaxLen);

	return *this;
}

Looks like a recursive call to me. There is an unused static method, padLeft_, that may be what you meant to call.