繁体   English   中英

数组长度变量-c

[英]array length variable - c

我在写一个ac程序,我注意到每当我用const变量声明数组长度时(const size_t MAX_LEN = some number ),它就会向我发送错误。 另一方面,当我使用(#define MAX_LEN = some number )作为我的数组长度声明时,它工作得很好。

我得到的确切错误是:LinSeperator.c:45:2:错误:使用了可变长度数组'arr'[-Werror = vla] double theAns,arr [MAX_LEN]; ^

谁能帮我弄清楚为什么会这样?

编辑:这是我的代码:这是我的LinSeperatorHelperFunc.h:

#pragma once
#include <stdio.h>

const size_t MAX_LEN = 199;

typedef struct Orange
{
double arr[MAX_LEN];
int tag;
}orange;


void learnProg(orange *o, double w[], int d);

void filePrinter(const char *output, FILE **fileIn, int d, double w[]);

这是我的.c文件:

#include "LinSeperator.h"
#include "LinSeperatorHelperFunctions.h"
#define NEG_ONE (-1)
#define NegToPos (2)




void LinSeperator(const char *In, const char *Out){
FILE * input;
orange o;
int d , pos, neg ,i , j;
//initializing the hypothesis vector as requested in step 1
double w[MAX_LEN];
for(i = 0 ; i<MAX_LEN ; i++){
    w[i] = 0;
}
input = fopen(In,"r");
if(input == NULL){
    printf("file doesnt exists");
    return;
}
fscanf(input, "%d %d %d", &d , &pos, &neg);

for(i = 0; i<pos+neg ; i++){
    o.tag = i<pos ? 1: -1;

    for(j = 0 ; j<d ; j++){
        fscanf(input, "%lf", &o.arr[j]);
        //removing ',' from being scanned
        if(j!= d-1){
            fgetc(input);
        }
    }
    learnProg(&o,w,d);
}
filePrinter(Out, &input, d, w);
fclose(input);

 }

void filePrinter(const char* out, FILE **in, int d, double w[]){
int i;
double theAns, arr[MAX_LEN];
FILE *output = fopen(out, "w");
if (output == NULL){
    printf("couldnt write to the current file");
    return;
}
while(!feof(*in)){

    for (i=0; i<d; i++) {
        fscanf((*in), "%lf", &arr[i]);
        if(feof(*in))//if we finished the checked vectors we should finish the file and the function 
        {
            fclose(output);
            return;
        }
        //preventing from reading the "," between each col
        if(i!=d-1){
            fgetc(*in);
        }
    }
    theAns=0;
    for (i=0; i<d; i++){
        theAns+=arr[i]*w[i];
    }
        //if ans >=0 print 1 to file else -1
    fprintf(output, "%d\n", NEG_ONE+NegToPos*(theAns>=0)); 
}
fclose(output);
  }




 //the learning progress algo
  void learnProg(orange *o, double w[], int d){
int i, negOrPos = (*o).tag;
double theAns = 0;
for(i = 0; i<d ; i++){
    theAns += ((*o).arr[i] * w[i]); //2.1
}
//has the same sign
if( (negOrPos * theAns) > 0 ){  //2.2
    return ;
}
else{
  for(i = 0; i<d ; i++){
      w[i] += (negOrPos * (*o).arr[i]);
  }
}
}

在C中, const不创建编译时常量。 它只是创建一个只读变量。 区别很重要。

使用时:

#define MAX_LEN 701

这是给预处理器的指令,用701替换所有出现的MAX_LEN 当编译器获取您的代码时,只看到数字常数。

C 90标准仅允许声明数字常数长度的数组。 但是,如果要使用C 99,则可以使用可变长度数组。

使用gcc ,您可以使用--std=c99--std=c90来设置编译代码所--std=c90标准。

这只是语言的限制。 您不能在数组声明中使用const size_t。 例如:

const size_t dimensions = 3;
size_t reality[dimensions]={3,8,12};

它不会编译,但是

#define dimensions 3
size_t reality[dimensions]={3,8,12};

作品。

静态绑定数组的大小需要为常量表达式,但不幸的是,在C语言中,这仅是文字常量或sizeof表达式之类的东西,而并非const型变量。 您可以尝试使用动态分配的指针数组 ,但是使用起来更复杂。

正如Lundin所说,从技术上讲,标准C99表示您不能向VLA提供初始化程序列表。 参考这里

这取决于声明数组的位置。

如果在文件作用域(任何函数之外)声明了数组大小,则数组大小必须是一个常量表达式,例如#define原始数字。 不幸的是,C并未将const变量视为常量表达式。

如果在本地范围内声明了数组,则问题仅在于您使用的是太旧的编译器,或者您配置了错误的GCC。 如果是GCC,请告诉它根据C标准语言编译代码: gcc -std=c11 -pedantic-errors -Wall -Wextra

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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