arrayscvariable-declaration

Variably modified 'data' at file scope error in C


I have the following C code here to create a command with a function called ccipher. It allows the user to execute the command and then enter a letter (a number afterwards if -s is entered for a key), like if the user enters ./ccipher -s 3 file.txt, it translates the contents in the file to a Caesar cipher with a shift of 3.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>

const int SIZE = 1024;
char data[SIZE], temp;
int key, count;

void getmessage() {
    /*
    printf("Enter a String: \t");
    scanf("%[^\n]s", data);
    */

    strncpy(data, "Hello World\n", sizeof("Hello World\n"));
}

void key_input() {
    /*
    printf("Enter a Key: \t");
    scanf("%d", &key);
    */

    key = 5;
}

void caesar_cipher_encryption() {
    for (count = 0; data[count] != '\0'; count++) {
        temp = data[count];
        if (temp >= 'a' && temp <= 'z') {
            temp = temp + key;
            if (temp > 'z') {
                temp = temp - 'z' + 'a' - 1;
            }
            data[count] = temp;
        } else if (temp >= 'A' && temp <= 'Z') {
            temp = temp + key;
            if (temp > 'Z') {
                temp = temp - 'Z' + 'A' - 1;
            }
            data[count] = temp;
        }
    }

    /*
    printf("%s\n", data);
    */
}

void caesar_cipher_decryption() {
    for (count = 0; data[count] != '\0'; count++) {
        temp = data[count];
        if (temp >= 'a' && temp <= 'z') {
            temp = temp - key;
            if (temp < 'a') {
                temp = temp + 'z' - 'a' + 1;
            }
            data[count] = temp;
        } else if (temp >= 'A' && temp <= 'Z') {
            temp = temp - key;
            if (temp < 'A') {
                temp = temp + 'Z' - 'A' + 1;
            }
            data[count] = temp;
        }
    }

    /*
    printf("\nDecrypted Message:\t%s\n", data);
    */
}


int main (int argc, char *argv[])
{
    int lineNum = 0, endLine = 0, tabReplace = 0, args = argc, num = 1, eolFlag=0, numRead=0, j=0, fd;
    int Encrypt = 0, Decrypt = 0;
    char buf[SIZE];
    char c;
    int shift = 0;

    // Check for no arguments
    if(argc == 1)
    {
            printf("Usage: %s -EnTsr file.txt\n", argv[0]);
            exit(0);
    }

    // Parse arguments for switches
    while(args--)
    {
        if(argv[args][0] == '-')
        {
            int switchLen = (int)strlen(argv[args]);
            while(switchLen--)
            {
                if(argv[args][switchLen] == 'n')
                    lineNum = 1;
                if(argv[args][switchLen] == 'E')
                    endLine = 1;
                if(argv[args][switchLen] == 'T')
                    tabReplace = 1;
                if(argv[args][switchLen] == 'r')
                    Decrypt = 1;
                if(argv[args][switchLen] == 's')
                {
                    Encrypt = 1;
                    key = strtol(argv[args+1], NULL, 10);
                }
            }
        }
    }

    if (Encrypt != 1)
    {
        printf("Nothing to encrypt, bye\n");
        return 0;
    }

    // Open file
    fd = open(argv[argc-1], O_RDONLY);
    if (fd == -1)
    {
        perror(argv[argc-1]);
        exit(1);
    }

    // Read file
    //while((numRead = read(fd, buf, SIZE)) > 0)
    while((numRead = read(fd, data, SIZE)) > 0)
    {
        caesar_cipher_encryption();
        if (Decrypt == 1)
            caesar_cipher_decryption();
        strcpy(buf, data);

        // Parse buffer
        for(j=0; j<numRead; j++)
        {
            c = buf[j];
            if(lineNum && (eolFlag || num == 1))
            {
                printf("     %d  ", num++);
                eolFlag = 0;
            }
            if(tabReplace && c == '\t')
            {
                printf("^I");
            }
            else if(endLine && c == '\n')
            {
                printf("$\n");
            }
            else
            {
                printf("%c", c);
            }
            if(lineNum && c == '\n')
            {
                eolFlag = 1;
            }
        }
    }

    // Close file
    close(fd);
    return 0;
}

When I compiled this code today at puTTY, I got this error:

ccipher.c:9:6: error: variably modified 'data' at file scope
   9 | char data[SIZE], temp
     |

I am pretty new to the C language and I am trying to learn it and avoid errors, but for this one it is a bit complicated to solve. Does anyone have any ideas on how to avoid this error? I would appreciate it!


Solution

  • You can't use const qualified file scope variables as array dimensions. In C, const is a misnomer for read-only.

    To fix, use a macro:

    #define SIZE 1024