casting a pointer as a byte type

For reference here is my complete code

#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <cstring>
#define chrTstELen 4

using namespace std;
typedef int8_t byte;
void Send_Bytes(byte *message, uint16_t messageLen);

union OneWireAddress
{
	//uint64_t LongID;
	uint64_t LongLongID;
	uint32_t WordID[2];
	uint8_t byteID[8];
};
struct TempStruct
{
public:
	char Label[6];
	OneWireAddress SensorID;
	TempStruct();
	TempStruct(int);
	ReadTemp(float& quickTemp) const;
private:
	float Temperature;
	float MinTemp;
	float MaxTemp;
protected:
	int Anew;
};

struct CharTesting
{
public:
	char Txt1[chrTstELen];
	char Txt2[chrTstELen];
	CharTesting();
	void SetSunday(int BNDay);
private:
	char Txt3[chrTstELen];
	char Txt4[chrTstELen];
protected:
	char Txt5[chrTstELen];
	char Txt6[chrTstELen];
	int Txt7;
};
class CharTestingClass
{
public:
	char Txt1[chrTstELen];
	char Txt2[chrTstELen];
	CharTestingClass();
	void SetSunday(int BNDay);
private:
	char Txt3[chrTstELen];
	char Txt4[chrTstELen];
protected:
	char Txt5[chrTstELen];
	char Txt6[chrTstELen];
	int Txt7;
};

int main()
{
	//uint64_t DefaultWord= 0x1122334455667788;
	CharTesting CharTest;
	CharTestingClass NewCharTest;
	TempStruct CurrentSensors[8];
	CurrentSensors[1].SensorID.LongLongID = 0x1122334455667788LL;
	printf("LongWord %llx Word %x, Byte %x\n", CurrentSensors[1].SensorID.LongLongID ,CurrentSensors[1].SensorID.WordID[1], CurrentSensors[1].SensorID.byteID[7] );
	strcpy( CharTest.Txt2,"TUE");
	cout << "Sizeof Chartest " << sizeof(CharTest) << endl;
	int snTxt = 7;
	CharTest.SetSunday(snTxt);
	Send_Bytes((byte *) &CharTest, sizeof(CharTest));
	cout << "Sizeof Chartestclass " << sizeof(CharTestingClass) << endl;
	NewCharTest.SetSunday(snTxt);
	Send_Bytes((byte *) &NewCharTest, sizeof(NewCharTest));
    return 0;
}
void CharTesting::SetSunday(int BNDay)
{
	Txt7 = BNDay;
}
void CharTestingClass::SetSunday(int BNDay)
{
	Txt7 = BNDay;
}
CharTesting::CharTesting()
{
	strcpy(Txt1,"MON");
	strcpy(Txt2,"TUE");
	strcpy(Txt3,"WED");
	strcpy(Txt4,"THU");
	strcpy(Txt5,"FRI");
	strcpy(Txt6,"SAT");
}

CharTestingClass::CharTestingClass()
{
	strcpy(Txt1,"MON");
	strcpy(Txt2,"TUE");
	strcpy(Txt3,"WED");
	strcpy(Txt4,"THU");
	strcpy(Txt5,"FRI");
	strcpy(Txt6,"SAT");
}

TempStruct::TempStruct()
{
	Temperature =45.3;
	MinTemp = 4.0;
	MaxTemp = 11;
}

/***************
FUNCTION:Send_Bytes
ARG:
  Pointer to a struct of any sort cast as a byte pointer
  Length of struct to sent.
PURPOSE:
  Send a struct of type Weather Data over Serial port with CRC32
RESOURCES: Weather Underground data, NTP Clock
RETURN: nil
TODO:
WIll need a weather data Packer Recieve 
with ability to resend a packet.

NOTES:  will initialise with a text character
****************/
void Send_Bytes(byte *message, uint16_t messageLen)
{
	uint8_t * BytePointer = (uint8_t *) message;
	for (int i = 0; i < messageLen; i ++)
	{
		//char ByteToPrint[3];
		printf("%02x", BytePointer[i]);
		//cout <<  ByteToPrint ;
		//Serial.write(BytePointer[i]);
	}
	cout << endl;
	BytePointer = (uint8_t *) message; // reset pointer
	for (int i = 0; i < messageLen; i ++)
	{
		//char ByteToPrint[3];
		byte SingledOut = ((char) BytePointer[i]);
		if (isalnum((int) SingledOut))
		{
			cout << SingledOut << " ";
		}
		else
		{
			cout << "  ";
		}
		//cout <<  ByteToPrint ;
		//Serial.write(BytePointer[i]);
	}
	cout << endl;
	//Serial.flush();
}

This is C++ not arduino C++ (yet)

I was discovering the way the compiler organises memory really so I can send a struct over the serial port. At the moment the line

Send_Bytes((byte *) &CharTest, sizeof(CharTest));

Invokes the Send_Bytes function, but has to cast the pointer to struct (or class as it turns out) as of type byte.
I wanted better way of doing this so I could pass ANY class or struct but I was struggling with the correct syntax. Can you even use a typecast in a function definition?
The purpose of sending the actual data type of the struct or class was so that I could retain the structure of the class in the subroutine, as I have already (in other code) set up a CRC32 as the final 4 bytes of the struct.

The other stuff was me experimenting with uint64_t and long longs. It generated a warning on gcc, but seemed to run ok not sure why it was giving me all the warnings. Finally I was trying to work out the difference between classes and structs, and to my surprise at this simple level I couldn’t find one yet. I haven’t really read up about OOP and Heirachies etc so maybe I will find some differences then.
I have googled it but apart from the default public/private of the native data I’ve read about differences in inherited classes but I don’t know what that means yet :slight_smile:

Thanks for anyone who might help.
Eventually I will combine all the code into a class or a struct (whichever seems appropriate) like a good little programmer should.
Raj
addit: I was also attempting go convert a onewire address into a single 64bit number for quick searches through a list of addresses. I created a union and used memcpy to copy the uint8_t bytes into it but I was running into bigendian and littlendian problems. So if anyone knows a way or resolving this it would help :slight_smile:

(tmpDevAddr[4] << 24) + (tmpDevAddr[5] << 16) + (tmpDevAddr[6] << 8) + tmpDevAddr[7];
just feels so ugly

The way to make a pointer generic is to make it a pointer to void.

Thanks for the answer
so does that mean a function definition like:

Sendbytes(void *message)
{}

rajdarge:
Thanks for the answer
so does that mean a function definition like:

Sendbytes(void *message)
{}

You would need a lot more than that, since the function would have no way to know the length of the data that message points to.

ok one step closer...

Send_bytes(void *message, uint16_t messageLen);

?

but I still won't be able to access the actual data of the struct/class from within the function will I?

such as message.crc32

but I would in a class I think?

Yes, of course you can access the data. That is the usual reason for passing a pointer. messageLen should be a size_t.