Nested Pointers

Hi,

I've been programming for a bit and STILL have troubles with my pointers. I have a piece of code I am editing and wish to keep conformance with how all other functions are formatted. Unfortunately, this means I need to pass an array through 3 functions before it gets written out the serial port. Somewhere I'm losing the contents of the array and using an address and am trying to figure out where and how to fix it. And yes, using the serial output function directly works but I can't do that in the actual code.

I have 3 classes, actuator, thruster, and serial.

This is called over and over in an actuator class function:
allThrusters->SendCommand(SerOut1, 4);

allThrusters is a thruster class object.

SerOut1 being float SerOut1[4]={0,0,0,0};

and: int SendCommand(void *command, int length);

Inside SendCommand:
1
2
3
4
5
6
nt thruster::SendCommand(void *command, int length)
{
	int nwrote;
	nwrote = myserial->WriteSerial(command, length);
	return nwrote;
}


myserial is a serial class object.

where: int WriteSerial(void* buff, int length);

The send is successfull if I do:
1
2
3
4
5
6
7
8
int thruster::SendCommand(void *command, int length)
{
	int nwrote;
	char test[]="Hello world!\n";
	nwrote = myserial->WriteSerial(test,13);
	//nwrote = myserial->WriteSerial(command, length);
	return nwrote;
}


I get Hello World! out of the serial port. So Im doing something wrong with passing the array through the pointers. Can anyone help me out? The array going into the original function (SerOut1) is fine and has the values I am expecting in it (viewed with a printf).

Thanks!
1
2
3
float SerOut1[4]={0,0,0,0};
//allThrusters->SendCommand(SerOut1, 4);
allThrusters->SendCommand(SerOut1, sizeof(SerOut1)); //of n*sizeof(float) 
I suppose that in your write function you are casting the void * to a char *.
I think it will be clearer if you do int thruster::SendCommand(char *command, int length) so you will see the cast in the caller (like in ios::read ios::write)
Actually:

1
2
3
4
5
6
7
8
9
10
11
for (tries = 0; tries < PORT_MAX_TRIES; tries ++)

	{

		nbytes = write(fd, buff, length);

		if (nbytes > 0)

			break;

	}


The write function, from what I understand, accepts a void*. I don't think I need to cast it. Or is that the entire issue?
No, you don't need the cast. That was just syntax sugar.
The important part is that length expects the numbers of bytes of the block.
I added in the sizeof() component. Slightly different gibberish (same hex values with extra zeros) but still a no go.
Okay-Im very confused now.

When I do:
1
2
char test[]="Hello world!\n";
allThrusters->SendCommand(test, sizeof(test));


This is successful.
but
1
2
 float SerOut1[4]={0,0,0,0};
allThrusters->SendCommand(SerOut1, sizeof(SerOut1));


is not. Am I misunderstanding the use of the write function? I've tried to read th output in ascii and hex. None of the numbers coming out would make sense, but the character string goes through okay. Is this some kind of conversion from the floats to the value of the float as a character?
Are you sure it is gibberish?
I didn't found the write function but I suppose that it will write the data in binary mode. Try to read it again or use an hexadecimal editor to check the output.
Topic archived. No new replies allowed.