2

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

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。可能是简单的铸造?

再次感谢

4

1 回答 1

5

当数组标识符不用作sizeof_Alignof一元运算&符时,将衰减为指针,并且不是左值。这意味着您不能像在此行中那样opt->filename使用运算符进行分配:=

opt->filename = filename;

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

  • 定义opt->filenamechar *。请注意,opt->filenamefilename. 否则,行为未定义。
  • 定义opt->filanamechar[256]并使用strcpy(from <string.h>)。

例如:

#include <string.h>

strcpy(opt->filename, filename);

一些参考资料:

  • 数组和左值:

C11 (n1570),第 6.3.2.1 节左值、数组和函数指示符

除非它是运算sizeof符、运算符_Alignof或一元&运算符的操作数,或者是用于初始化数组的字符串文字,否则类型为 ''array of type'' 的表达式将转换为类型为 '' 的表达式指向 type'' 的指针,它指向数组对象的初始元素并且不是左值

  • 赋值运算符:

C11 (n1570), § 6.5.16 赋值运算符

赋值运算符应具有可修改的左值作为其左操作数。

于 2013-03-23T09:03:34.100 回答