2017-09-14 171 views
-3

我有问题invalid conversion from 'char*' to 'char' [-fpermissive]。 在这里,我将数据转移一位数。char从'char *'无效转换为'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并且不能在Debug中工作。所以我需要使用null终端。我正在分享我正在执行的代码。

#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。 我还没有完成。

感谢

+6

我很惊讶这是您显示的代码唯一的问题。数组初始值设定项必须是编译时常量。你不能使用运行时变量来初始化一个数组(就像你使用'text'一样)。 –

+3

此外,当你做'array [3] =“5052.4318”'你正在索引*出界*!数组中的最后一个索引是'2'。 –

+2

'array'是一个指针数组,'array [3]'超出了界限。你也不能把''5052.4318“'分配给'char *',你应该用'malloc'和'strcpy'来代替。 – Groo

回答

0

我不能完全肯定你想什么来实现,但也存在一些问题与您的代码的第一部分。你的初始化行并不是真的正确。

我认为你试图“除以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++编译器。这件事你一定要小心。

0
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

int main() 
{ 
    char text[30]="5052.4318",temp; 
    int text_len = strlen (text),i; 
    //printf("%s\n%d\n",text,text_len); 
    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; 
} 

您不需要使用额外的字符数组来实现所需的结果。

相关问题