gpt4 book ai didi

c++ - 使用指向 "return"多维数组的函数指针

转载 作者:行者123 更新时间:2023-11-30 03:46:04 25 4
gpt4 key购买 nike

我想将字符数组 micPointsChar[] 传递给函数 initMicPoints() 并将其解析为多维数组 micPoints。我能够使用一维数组成功地做到这一点:

char micPointsChar[30 + 1] = {};
float *initMicPoints(char micPointsChar[], float micPoints[3]);

int main()
{
// Read in mic points from file
char micPointsChar[40] = "2,3.343,4.432\n";
float micPoints[3] = {};
float *newMicPoints = initMicPoints(micPointsChar, micPoints);
for (int i = 1; i <= 3; i++)
{
Serial.print(newMicPoints[i]);
Serial.print("\n");
}
return 0;
}


float *initMicPoints(char micPointsChar[], float micPoints[3])
{
static int i = 1;
static int micNum = 1;
static int numMics = 1;
float coordinateDec = 0;

char *coordinate = strtok(micPointsChar, ",\n");
coordinateDec = atof(coordinate);
while (micNum <= numMics)
{
while (i <= ((micNum * 3)) && (coordinate != NULL))
{
if (i == ((micNum * 3) - 2))
{
micPoints[1] = coordinateDec;
}
else if (i == ((micNum * 3) - 1))
{
micPoints[2] = coordinateDec;
}
else if (i == ((micNum * 3) - 0))
{
micPoints[3] = coordinateDec;
}
coordinate = strtok(NULL, ",\n");
coordinateDec = atof(coordinate);
i++;
}
micNum++;
}
return micPoints;
}

这输出预期的:

2.00
3.34
4.43

但是,当我更改代码以处理多维数组时,micPoints[360][3]:

char micPointsChar[30 + 1] = {};
float *initMicPoints(char micPointsChar[], float micPoints[360][3]);

int main()
{
// Read in mic points from file
char micPointsChar[40] = "2,3.343,4.432\n";
float micPoints[360][3] = {};
float *newMicPoints = initMicPoints(micPointsChar, micPoints);
static int i = 0;
for (i = 1; i <= 3; i++)
{
Serial.print(*newMicPoints[i][0]);
Serial.print("\n");
Serial.print(*newMicPoints[i][1]);
Serial.print("\n");
Serial.print(*newMicPoints[i][2]);
Serial.print("\n");
}
return 0;
}


float *initMicPoints(char micPointsChar[], float micPoints[360][3])
{
static int i = 1;
static int micNum = 1;
static int numMics = 1;
float coordinateDec = 0;

char *coordinate = strtok(micPointsChar, ",\n");
coordinateDec = atof(coordinate);
while (micNum <= numMics)
{
while (i <= ((micNum * 3)) && (coordinate != NULL))
{
if (i == ((micNum * 3) - 2))
{
micPoints[i][0] = coordinateDec;
}
else if (i == ((micNum * 3) - 1))
{
micPoints[i][1] = coordinateDec;
}
else if (i == ((micNum * 3) - 0))
{
micPoints[i][2] = coordinateDec;
}
coordinate = strtok(NULL, ",\n");
coordinateDec = atof(coordinate);
i++;
}
micNum++;
}
return micPoints;
}

我收到一个编译时错误:

cannot convert 'float (*)[3]' to 'float*' in return

我是不是把它弄得太复杂了?返回多维数组的最佳方法是什么?

最佳答案

首先,不幸的是

float *initMicPoints(char micPointsChar[], float micPoints[360][3])

被视为

float *initMicPoints(char* micPointsChar, float (*micPoints)[3])

您可以通过引用传递以保持大小:

float *initMicPoints(char* micPointsChar, float (&micPoints)[360][3])

然后当你返回 micPoints

返回类型应为 float (&)[360][3]float (&)[360][3]

这给了一个丑陋的

float (&initMicPoints(char* micPointsChar, float (&micPoints)[360][3]))[360][3]

在调用站点:

float (&newMicPoints)[360][3] = initMicPoints(micPointsChar, micPoints);

首选语法更清晰的 std::arraystd::vector

关于c++ - 使用指向 "return"多维数组的函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34345060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com