我对invalid conversion from 'char*' to 'char' [-fpermissive]
有疑问。
在这里,我将数据移动一位数。
char text[]="5052.4318" ,temp;
当我这样写时,好吧,它正在工作,但我需要从 array[3]
读取数据。
我该如何处理这个问题?
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
char *array[3];
array[3]="5052.4318";
char text[]={array[3]} ,temp;
int text_len = strlen (text),i;
for (i =0; i <=text_len - 1; i++)
{
if (text[i] == '.')
{
temp = text[i-1];
text[i-1] = text[i];
text[i] = temp;
}
}
printf ("%s\n", text);
return 0;
}
工作
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (void) {
char *array[1] = {"5052.4318"};
size_t text_len = strlen(array[0]);
char text[text_len + 1], temp;
int i;
strcpy(text, array[0]);
for (i=0; i <=text_len - 1; i++){
if (text[i] == '.') {
temp = text[i-1];
text[i-1] = text[i];
text[i] = temp;
}
}
printf ("%s\n", text);
}
在这里,我尝试读取 GPS 数据,该数据是 GPRMC,所以首先我需要在转换为谷歌地图格式后解析这些数据(纬度需要经度)。
$GPRMC,093612.000,A,5052.43525,N,00440.11204,E,0.0,0.0,130917,,,A*6C
5052.43525 是纬度值。首先,我需要像这样移动数据 50.5243525。再次移动>>除以 60=> 52.43525/60=0.87392083 后。 所以结果应该是 50.87392083。另一个问题是我不应该使用 atof 命令将字符串转换为浮点值。因为我使用的是Coocox并且不在调试中工作。也许我需要使用空终端。我正在分享我正在做的这段代码。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
//char buf[]
="$GPRMC,121212,A,4807.038,N,01131.000,E,022.4,084.4,030611,003.1,W*6A";
char T[100];
sprintf(T,
"%s","$GPRMC,093612.000,A,5052.43525,N,00440.11204,E,0.0,0.0,130917,,,A*6C");
printf(T);
char *buf[]={T};
int i = 0;
char *p = strtok (*buf, ",");
char *array[12];
while (p !=0)
{
array[i++] = p;
p = strtok (0, ",");
}
for (i = 0; i < 11; ++i) {
array[i];
printf("%s\n",array[i]);
}
//char *array[3] = {"5052.4318"};
size_t text_len = strlen(array[3]);
char text[text_len +1], temp;
int i1;
strcpy(text, array[3]);
for (i1 =0; i1 <=text_len - 1; i1++)
{
if (text[i1] == '.')
{
temp = text[i1-1];
text[i1-1] = text[i1];
text[i1] = temp;
}
}
for (i1 =0; i1 <=text_len - 1; i1++)
{
if (text[i1] == '.')
{
temp = text[i1-1];
text[i1-1] = text[i1];
text[i1] = temp;
}
}
char *buf1[]={text};
int i3 = 0;
char *p1 = strtok (*buf1, ".");
char *array1[2];
while (p1 !=0)
{
array1[i3++] = p1;
p1 = strtok (0, ".");
}
for (i3 = 0; i3 < 2; ++i3) {
array1[i3];
} double d;
float m;
int k=0;
d= (atof(array1[1])/6000);
char s[1]= {d};
m=(atof(array1[0]));
printf("%f\n",m);
printf("%lf\n",d);
return 0;
}
结果应为 50.87392083。 我还没有完成。
谢谢
最佳答案
我不完全确定您想要实现什么目标,但是您的代码的第一部分存在一些问题。您的初始化行并不正确。
我认为您正在尝试将字符串“除以 10”。保留您的算法,我修复了一些问题以对其进行编译。
// #include <iostream> // This is C not C++. Also you include stdio.h...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
char array[] = "5052.4318"; // This initialization
// one of your errors
char * text = array + 3; // I don't know why you are pointing
// fourth element of the array, but you may do
// in this way
// Let's say I prefer to start from the starting of
// the array
text = array;
printf ("%s\n", text);
int text_len = strlen(text);
for (int i = 1; i <= text_len - 1; i++) { // You will have some problem
// if you start from 0...
if (text[i] == '.') {
char temp; // temp is used only here, so let's define it in the
// only scope that needs it.
temp = text[i - 1]; // This -1 is the reason why you may start
// the for from 1, instead of 0.
text[i - 1] = text[i];
text[i] = temp;
}
}
printf ("%s\n", text);
return 0;
}
打印出来:
5052.4318
505.24318
这就是你想要实现的目标吗?
for
从 1 开始,用于处理像 ".1234"
这样的字符串。
另外:-fpermissive
是控制 C++ 编译器“方言”的标志。这不是您在 C 中编译时看到的东西。由于您包含了 iostream,您的编译器会自动切换到 C++ 编译器。你在这件事上必须非常小心。
关于从 'char*' 到 'char' 的 Char 无效转换 [-fpermissive],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46218851/