C 中文件范围错误的可变修改 'data'
Variably modified 'data' at file scope error in C
我这里有以下 C 代码来创建一个带有名为 ccipher 的函数的命令。它允许用户执行命令,然后输入一个字母(如果为密钥输入 -s,则之后输入一个数字),就像用户输入 ./ccipher -s 3 file.txt 它会翻译文件中的内容转换为凯撒密码 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] != '[=11=]'; 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] != '[=11=]'; 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 ARGS
if(argc==1)
{
printf("Usage: %s -EnTsr file.txt\n",argv[0]);
exit(0);
}
//PARSE ARGS 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;
}
今天在puTTY编译这段代码时,出现了这个错误:
ccipher.c:9:6: error: variably modified 'data' at file scope
9 | char data[SIZE], temp
|
我是C语言的新手,我正在努力学习它并避免错误,但是对于这个问题来说,解决起来有点复杂。有没有人对如何避免此错误有任何想法?我将不胜感激!
您不能使用 const
限定的文件范围变量作为数组维度。在 C 语言中,const
是 read-only 的误称。
要修复,请使用宏:
#define SIZE 1024
我这里有以下 C 代码来创建一个带有名为 ccipher 的函数的命令。它允许用户执行命令,然后输入一个字母(如果为密钥输入 -s,则之后输入一个数字),就像用户输入 ./ccipher -s 3 file.txt 它会翻译文件中的内容转换为凯撒密码 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] != '[=11=]'; 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] != '[=11=]'; 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 ARGS
if(argc==1)
{
printf("Usage: %s -EnTsr file.txt\n",argv[0]);
exit(0);
}
//PARSE ARGS 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;
}
今天在puTTY编译这段代码时,出现了这个错误:
ccipher.c:9:6: error: variably modified 'data' at file scope
9 | char data[SIZE], temp
|
我是C语言的新手,我正在努力学习它并避免错误,但是对于这个问题来说,解决起来有点复杂。有没有人对如何避免此错误有任何想法?我将不胜感激!
您不能使用 const
限定的文件范围变量作为数组维度。在 C 语言中,const
是 read-only 的误称。
要修复,请使用宏:
#define SIZE 1024