更新 16.4。 11:02
你好汤姆!我尝试了您提供的代码更改,它输出了有趣的值。
我做了一些实验,改变了 for 循环的大小和 txString[]
中与 i
相乘的数字,这就是我观察到的:
**CONDITIONS:**
char tamp[20];
char txString[32];
for (unsigned int i=0; i<3; i++){
char tamp[20];
dtostrf(coordArray[i], 7, 2, tamp); //convert one of your coordinates
for (unsigned int j=0;j<5;j++) {txString[5*i+j] = tamp[j];} //concatenate it inside txString
}
**OUTPUT:**
10:32:06.052 -> Yaw:
10:32:06.052 -> 311.44
10:32:06.052 -> , Pitch:
10:32:06.052 -> -0.03
10:32:06.052 -> , Roll:
10:32:06.052 -> 179.99
10:32:06.052 -> *****txString****
10:32:06.052 -> 311. -0. 179.
10:32:06.052 -> ************
----------------------------
dtostrf(coordArray[i], 7, 2, tamp);
for (unsigned int j=0;j<6;j++) {txString[6*i+j] = tamp[j];
OUTPUT:
10:43:55.371 -> Yaw:
10:43:55.371 -> 310.51
10:43:55.371 -> , Pitch:
10:43:55.371 -> 0.38
10:43:55.371 -> , Roll:
10:43:55.371 -> -179.64
10:43:55.371 -> *****txString****
10:43:55.371 -> 310.5 0.3-179.6ART HELMET //Here it outputs name of the BLE device I gave it in upper part of code I didnt mention here BLEDevice::init("SMART HELMET");
10:43:55.371 -> ************
-----------------------------
dtostrf(coordArray[i], 6, 2, tamp); //here I changed the string width and it outputted only Yaw data
for (unsigned int j=0;j<7;j++) {txString[7*i+j] = tamp[j];
OUTPUT:
10:49:58.993 -> Yaw:
10:49:59.027 -> 311.95
10:49:59.027 -> , Pitch:
10:49:59.027 -> 1.91
10:49:59.027 -> , Roll:
10:49:59.027 -> -178.10
10:49:59.027 -> *****txString****
10:49:59.027 -> 311.95
10:49:59.027 -> ************
--------------------------------
dtostrf(coordArray[i], 16, 2, tamp);
10:58:43.547 -> Yaw:
10:58:43.547 -> 314.31
10:58:43.547 -> , Pitch:
10:58:43.547 -> -0.08
10:58:43.547 -> , Roll:
10:58:43.547 -> 179.93
10:58:43.547 -> *****txString****
10:58:43.547 -> HELMET
10:58:43.547 -> ************
最佳答案
返回本地数组(在函数内部声明)会给您带来问题,因为一旦函数结束它就会超出范围。要返回本地数组,必须动态分配它,以便它可以比函数更长寿
float* result (float x, float y, float z)
{
float* coordArray=malloc(3*sizeof(float));
coordArray[0] = x;
coordArray[1] = y;
coordArray[2] = z;
return coordArray;
}
关于无法在赋值中将 'float*' 转换为 'float',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55680701/