Thread: Constant Expression expected when I try to pass a char variable to another char varia

  1. #1
    Registered User
    Join Date
    Dec 2022
    Posts
    1

    Angry Constant Expression expected when I try to pass a char variable to another char varia

    I have been trying to create a routine that can pass data from one function to another to save user input data however passing the variable data from char save to char data_out[] leads to a Constant Expression expected error on said line of code. This code also causes a { expected error but I am unsure where it exactly wants the curly brackets since I know my code is correctly formatted.

    Code:
    int integer1;
    int integer2;
    int integer3;
    int option;
    char save;
    char c;
    FILE *fp;
    
    
    void get2nums() {
        printf("FIRST NUMBER?\n");
        scanf("%d", &integer1);
        printf("SECOND NUMBER?\n");
        scanf("%d", &integer2);
    }
    
    
    void addition() {
         get2nums();
         integer1 + integer2 == save;
         printf("%d", integer1 + integer2);
         printf("\n");
    
    
    static void savedata(void) {
         char data_out[] = save;
    
    
         _filetype = 's';
         if ((fp = open ("CALCDATA", "w")) == 0) {
              printf("File could not be opened\n\r");
              exit(1);
         }
         fwrite (data_out, 1, sizeof (data_out), fp);
         fclose (fp);
    }
    Some steps I have taken to resolve this issue include adding the curly brackets around the save variable but they only resolved the { expected error but not the constant expression error. I have tweaked the code in a variety of ways to try to find the cause of the problem but that only caused more errors. My intention for the the code to be able to save data from the addition function if the user requests it later on. Any info will help, thanks!
    Note I included the text file on accident and don't know how remove it, it is not relevant.
    Attached Files Attached Files
    Last edited by assortedkingded; 12-08-2022 at 10:08 AM. Reason: Clarification on intentions

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Just get rid of the square brackets after data_out.
    Then the fwrite would be
    Code:
    fwrite(&data_out, sizeof(data_out), 1, fp);
    However, you should not be using global variables. For example, instead of this:
    Code:
    int integer1, integer2;
     
    void get2nums() {
        printf("FIRST NUMBER?\n");
        scanf("%d", &integer1);
        printf("SECOND NUMBER?\n");
        scanf("%d", &integer2);
    }
     
    int main() {
        getnums();
        printf("%d %d\n", integer1, integer2);
        return 0;
    }
    it should be more like this:
    Code:
    void get2nums(int *integer1, int *integer2) {
        printf("FIRST NUMBER?\n");
        scanf("%d", integer1);
        printf("SECOND NUMBER?\n");
        scanf("%d", integer2);
    }
     
    int main() {
        int integer1, integer2;
        getnums(&integer1, &integer2);
        printf("%d %d\n", integer1, integer2);
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expected expression before char
    By tmac619619 in forum C Programming
    Replies: 8
    Last Post: 10-23-2012, 03:58 PM
  2. Assign constant char to another char variable
    By effup in forum C Programming
    Replies: 4
    Last Post: 10-04-2012, 07:43 PM
  3. expected constant expression?
    By GroogFish in forum C Programming
    Replies: 3
    Last Post: 05-08-2012, 12:17 AM
  4. Expected Constant Expression in one compiler
    By jimmyjamesii in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2005, 01:47 AM
  5. expected constant expression
    By Brian in forum C Programming
    Replies: 4
    Last Post: 12-03-2003, 03:30 PM

Tags for this Thread