Char从“Char*”到“Char”[-fpermissive]的转换无效
我在从“char*”到“char”[-fppermissive]的无效转换中遇到问题。 在这里,我将数据移动一位Char从“Char*”到“Char”[-fpermissive]的转换无效,c,arrays,string,pointers,char,C,Arrays,String,Pointers,Char,我在从“char*”到“char”[-fppermissive]的无效转换中遇到问题。 在这里,我将数据移动一位 char text[]="5052.4318" ,temp; 当我这样写的时候,好的,它正在工作,但是我需要从数组中读取数据[3]。 我如何处理这个问题 #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> int main (
char text[]="5052.4318" ,temp;
当我这样写的时候,好的,它正在工作,但是我需要从数组中读取数据[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;
}
5052.43525是纬度值。首先我需要像这样移动数据50.5243525。在除以数字60=>52.43525/60=0.87392083之后再次移动>>。
所以结果应该是50.87392083。另一个问题是我不应该对字符串使用atof命令来浮动值。因为我使用的是Coocox,没有在调试中工作。也许我需要使用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。
我还没有完成
谢谢我不完全确定您想要实现什么,但是您的代码的第一部分有一些问题。您的初始化行实际上不正确 我想你是想把你的字符串除以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;
}
这就是你想要实现的吗
for从1开始处理字符串,如.1234
也是:一个控制C++编译器的方言的标志。这不是你在C.编译时看到的东西,因为你包含了IOSROW,编译器自动切换到C++编译器。在这件事上你必须非常小心
#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;
}
您不需要使用额外的字符数组来实现所需的结果。我很惊讶,这是您显示的代码的唯一问题。数组初始值设定项必须是编译时常量。您不能像使用文本一样使用运行时变量初始化数组。此外,当您使用数组[3]=5052.4318时,您的索引超出了范围!数组中的最后一个索引是2。数组是指针数组,数组[3]超出范围。您也不能将5052.4318分配给char*,应改为malloc和strcpy.char*数组[1];数组[0]=5052.4318;这是给,也是同样的错误,我用strcpy,但没有工作。我要试试马洛克@葛哈德:是的,这实际上是一个问题,但问题是OP想要改变文字,所以它不起作用。
// #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
#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;
}