当我尝试编译我的代码时,我得到以下错误:
error #2168: Operands of '=' have incompatible types 'char [255]' and 'char *'.
error #2088: Lvalue required.
Run Code Online (Sandbox Code Playgroud)
我得到这些错误的同一行(即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;
Run Code Online (Sandbox Code Playgroud)
引发错误的函数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;
}
Run Code Online (Sandbox Code Playgroud)
无论如何,我相信我已经宣布两个文件名为255个字符,...编译器不会犯错..所以我想它是我:)我哪里出错了?我的想法是我有一个函数可以创建一个参数扫描,例如驱动力..我需要模拟来转储该数据的文件: - results1.txt - results2.txt - results3.txt
这提出了另一个问题,但我肯定找到了它的答案,谷歌......我怎样才能在c中从int转换为char.简单铸造可能吗?
再次感谢
当一个数组识别符不被用作sizeof,_Alignof或一元&运算符,衰减到一个指针,并且不是一个左值.这意味着你无法分配opt->filename与=你在这一行做操作:
opt->filename = filename;
Run Code Online (Sandbox Code Playgroud)
我可以看到两种解决方案.
opt->filename为char *.注意opt->filename不要在生命周期之外使用filename.否则,行为未定义.opt->filaname为char[256]和使用strcpy(从<string.h>).例如:
#include <string.h>
strcpy(opt->filename, filename);
Run Code Online (Sandbox Code Playgroud)
一些参考:
C11(n1570),第6.3.2.1节左值,数组和函数指示符
除非它是运算
sizeof符,_Alignof运算符或一元运算&符的操作数,或者是用于初始化数组的字符串文字,否则将类型为''数组'的数组的表达式转换为类型为''的表达式指向类型''的指针指向数组对象的初始元素,而不是左值.
C11(n1570),§6.5.16赋值运算符
赋值运算符应具有可修改的左值作为其左操作数.