当我尝试编译我的代码时,出现以下错误:
error #2168: Operands of '=' have incompatible types 'char [255]' and 'char *'.
error #2088: Lvalue required.
我在同一行(即 1044)和多行上得到这些错误,所以我想通过修复一个我可以修复其他的,所以让我复制你的代码。您可以跳过并只阅读注释以 ** 开头的行,以使其更容易:) 并以 <-- 我希望代码中的注释为您提供良好的服务:首先让我从定义类型 PRINTOPT 开始
typedef struct {
//UsePl signifies if the user would like to see the graphs without having to export data
//Thanks to PlPlot library.
int usePl;
//Feel free to customize and add to this struct
//for any simulation program you create.
//---- Note2self: probably change the graph bool to an array,
//as later you will have to print around 20 graphs or so
int thetaGraph; //Plot Theta VS Time
int omegaGraph; //Plot Omega VS Time
char filename[255]; //**I have declared it to be a 255 char. <============
int matlab; //0 no, not 0 yes;
} PRINTOPT;
引发错误的函数 int ReadPrintOpt(PRINTOPT *opt) { int input;
int usePl;
int thetaGraph;
int omegaGraph;
//**The result behind this def, i would like the user to input a filename
//To save his data in, <========================================================
char filename[255] = "Osc Motion and Chaos- Results"; //I have declared filename as char [255]
int matlab;
printf("\n----Print Options----\n");
printf("\nMENU (choose one of the following commands)\n");
printf("\n\t 1 - Display Graphs after Simulation\t\t\tCurrent Val\t\"%d\"",opt->usePl);
printf("\n\t 2 - Enable Theta vs Time Graph\t\tCurrent Val\t\"%d\"",opt->thetaGraph);
printf("\n\t 3 - Enable Omega vs Time Graph\t\tCurrent Val\t\"%d\"",opt->omegaGraph);
printf("\n\t 4 - Save Data in Matlab Format\t\tCurrent Val\t%d",opt->matlab);
printf("\n\t 5 - Filename for exported files\t\tCurrent Val\t%s",opt->filename);
printf("\n\n\t 0 - <DONE>\n>>");
scanf("%d",&input);
switch(input) {
case 0:
return 0;
case 5:
printf("Enter Filename: ");
fgets(filename, 255, stdin); //**i've been told to use this, saw it on another question
opt->filename = filename; //**In this part, opt is of type PRINTOPT
//I have been told that the name of an array, is actually
//a pointer to the first element, so why does this part
//give me this error -- Operands of '=' have incompatible types 'char[255] and [char*]
//although i've declared both as char[255];
break;
case 4:
printf("Enable Matlab (0 no, else yes): ");
scanf("%d",&matlab);
opt->matlab = matlab;
break;
case 1:
printf("Use this program to display plots (0 no, else yes): ");
scanf("%d",&usePl);
opt->usePl = usePl;
break;
case 2:
printf("Record Data for Graph of Theta (0 no, else yes): ");
scanf("%d",&thetaGraph);
opt->thetaGraph = thetaGraph;
break;
case 3:
printf("Record Data for Graph of Omega (0 no, else yes): ");
scanf("%d",&omegaGraph);
opt->omegaGraph = omegaGraph;
break;
default:
printf("Invalid Input!");
break;
}
return 1;
}
无论如何,我相信我已经将两个文件名都声明为 255 字符,.. 编译器不会出错.. 所以我认为是我 :) 我哪里出错了?这个想法是我有一个函数可以创建对参数的扫描,例如驱动力..我需要模拟来转储该数据的文件:- results1.txt - results2.txt - results3.txt
这引发了另一个问题,但我肯定会找到答案,谷歌......我如何在 c 中从 int 转换为 char。可能是简单的铸造?
再次感谢