简体   繁体   中英

String problems in C: operands of unequal types, char[255] and char*

When i try to compile my code i get the following errors:

error #2168: Operands of '=' have incompatible types 'char [255]' and 'char *'.
error #2088: Lvalue required.

i get these errors for the same line (ie 1044), and on a multiple of lines, so i figure by fixing one i can fix the others, so let me copy you the codes. You can skip and only read lines where comments start with ** just to make it easier :) and ending with <-- I hope the in code comments serve you well: first let me start by defining the type 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;

function which raises the error 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;
}

anyways, i believe i have declared both filename as 255 char, .. the compiler doesnt make mistakes.. so i figured it is me :) where did i go wrong? The idea is i have a function that creates a sweep over a parameters, such as the driving force.. and i need the simulation to dump files of that data: - results1.txt - results2.txt - results3.txt

which raises another question, but i surely find the answer to it, google... how can i transform from int to char in c. Simple casting probably?

thanks again

When an array identifier is not used as sizeof , _Alignof or unary & operator, decays to a pointer, and is not an lvalue. It means that you cannot assign opt->filename with the = operator as you do in this line:

opt->filename = filename;

I can see two solutions.

  • Define opt->filename as char * . Beware that opt->filename is not used outside the lifetime of filename . Otherwise, the behavior is undefined.
  • Define opt->filaname as char[256] and use strcpy (from <string.h> ).

For instance:

#include <string.h>

strcpy(opt->filename, filename);

Some references:

  • Array and lvalues:

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 .

  • Assignment operator:

C11 (n1570), § 6.5.16 Assignment operators

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM