gpt4 book ai didi

c - C : operands of unequal types, char[255] 和 char* 中的字符串问题

转载 作者:行者123 更新时间:2023-12-02 05:10:17 25 4
gpt4 key购买 nike

当我尝试编译我的代码时,出现以下错误:

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 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 个字符,..编译器不会出错..所以我认为是我:)我哪里做错了?我的想法是我有一个函数可以扫描参数,例如驱动力..我需要模拟来转储该数据的文件:- 结果1.txt- 结果2.txt- 结果3.txt

这提出了另一个问题,但我肯定能找到答案,谷歌...我怎样才能在 c 中从 int 转换为 char。可能是简单的类型转换?

再次感谢

最佳答案

当数组标识符未用作 sizeof 时, _Alignof或一元 &运算符,衰减为指针,并且不是左值。这意味着您不能分配 opt->filename=运算符,就像您在这一行中所做的那样:

opt->filename = filename;

我可以看到两个解决方案。

  • 定义opt->filename作为char * .当心 opt->filenamefilename 的生命周期之外不使用.否则,行为未定义。
  • 定义opt->filaname作为char[256]并使用 strcpy (来自 <string.h>)。

例如:

#include <string.h>

strcpy(opt->filename, filename);

一些引用资料:

  • 数组和左值:

C11 (n1570), § 6.3.2.1 Lvalues, arrays, and function designators

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue.

  • 赋值运算符:

C11 (n1570), § 6.5.16 Assignment operators

An assignment operator shall have a modifiable lvalue as its left operand.

关于c - C : operands of unequal types, char[255] 和 char* 中的字符串问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15585010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com