无法在赋值中将 'float*' 转换为 'float'

标签 c arrays pointers arduino bluetooth-lowenergy

更新 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/

相关文章:

c - 指向字符串字符的指针数组

c - 指针和cstring初学者的烦恼

php - 从一个表中选择行,其中存在来自另一表的数组的值

c - C-指向矩阵的指针

c - 有没有办法在 C 中循环遍历具有不同类型元素的结构?

c - 如何用 c 写入文本文件(具有预期功能)

php - 检查一个数组是否可以放入另一个数组

c++ - 在常数时间内提取子 vector

c - 如何让 fgets() 在开头忽略 a\n?

c - ‘s’ 的存储大小未知 + 指向指针的指针