Char无效转换为'char *'到'char'[-fpermissive]

时间:2017-09-14 12:14:35

标签: c arrays string pointers char

我遇到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.Again shift&gt;&gt;在60的分数之后=> 52.43525 / 60 = 0.87392083。 所以结果应该是50.87392083。另一个问题我不应该使用atof命令来使字符串浮点值。因为我正在使用Coocox而不在Debug.Maybe中工作,所以我需要使用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。 我还没完成。

谢谢

2 个答案:

答案 0 :(得分:0)

我不完全确定您要实现的目标,但代码的第一部分存在一些问题。你的初始化行不是很正确。

我认为你正试图除以10&#34;你的字符串。保持您的算法,我已修复了一些问题,以便将其编译。

// #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是一个控制&#34; dialect&#34;的标志。 C ++编译器。在C语言编译时,不会看到它。由于包含iostream,编译器会自动切换到C ++编译器。你必须非常小心这件事。

答案 1 :(得分: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;
}

您无需使用额外的字符数组即可获得所需的结果。