2017-09-14 18 views
-3

invalid conversion from 'char*' to 'char' [-fpermissive]に問題があります。 ここでは、データを1桁シフトしています。私はこのように書く時文字 'char *'から 'char'への無効な変換[-fpermissive]

char text[]="5052.4318" ,temp; 

、[OK]を、それが働いているが、私は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あるので、まず、私は(緯度経度を必要とする)のマップ形式をGoogleに変換する必要があります後にこれらのデータを解析する必要があります。

$GPRMC,093612.000,A,5052.43525,N,00440.11204,E,0.0,0.0,130917,,,A*6C 

5052.43525は緯度の値です。まず、このようなデータをシフトする必要があります。50.5243525.Again shift >> 60の除算後=> 52.43525/60 = 0.87392083。 結果は50.87392083になります。もう一つの問題は、atofコマンドを文字列の値として使用するべきではありません。私はCoocoxを使用しており、Debug.Maybeで動作していないので、私はヌル端末を使用する必要があります。私は進行中のこのコードを共有しています。

#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; 
} 

必要な結果を得るために余分な文字配列を使用する必要はありません。

関連する問題