C中的字符串问题:不等类型的操作数,char [255]和char *

时间:2013-03-23 08:59:51

标签: c string

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

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输入;

    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

提出了另一个问题,但我肯定会找到它的答案,google ... 我如何在c中从int转换为char。简单铸造可能吗?

再次感谢

1 个答案:

答案 0 :(得分:5)

当数组标识符未用作sizeof_Alignof或一元&运算符时,衰减为指针,而不是左值。这意味着您无法像在此行中那样为opt->filename运算符分配=

opt->filename = filename;

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

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

例如:

#include <string.h>

strcpy(opt->filename, filename);

一些参考文献:

  • 数组和左值:
  

C11(n1570),§6.3.2.1左值,数组和函数指示符

     

除非它是sizeof运算符,_Alignof运算符或者&运算符的操作数。   一元{{1}}运算符,或者是用于初始化数组的字符串文字,具有类型''数组类型''的表达式将转换为类型为''指向类型'的指针的表达式,指向初始值数组对象的元素和不是左值

  • 作业运营商:
  

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

     

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