C 错误 : invalid types 'double[int]' for array subscript

标签 c arrays

我正在创建一个程序来读取文件并将其数据存储在数组中,然后创建一个函数来计算其导数,能够读取文件并创建导数,但是当我尝试创建一个外部函数时计算函数的导数我遇到以下错误:“[error] invalid types 'double [int]' for array subscript” Ja 尝试用不同的方法解决它,但没有成功。

#include "biblioteca.h"

int main(){

FILE* fp;
fp = fopen("entrada.txt","rt");
if (fp == NULL) {
printf("Erro na abertura do arquivo!\n");
exit(1); }

int i; //grau do polinomio
fscanf(fp,"%d",&i);
printf("i = [%d]\n",i);
double mat[i-1][i+1];
int c=0; //colunas

while(c<i+1){
fscanf(fp,"%lf",&mat[0][c]);
c++;
}   

int h=i; //h grau do expoente
for(int l=0;l<i-1;l++){ //l linhas
int k=0; //contador
for(c=h;c>0;c--){
    mat[l+1][c-1]=mat[l][c]*(h-k);
    k++;
}
h--;
}

int k=0; //linhas
while(k<i){
for(c=0;c<i+1;c++){
    printf("%lf \t",mat[k][c]);}
k++;
printf("\n");
}

double x=mat[i-1][0]*-1/mat[i-1][1];
printf("x = %lf\n",x);

double x1;
x1=f(**mat,i,1,x);

system("pause");
return 0;
}

图书馆:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

double f(double mat,int i,int t,double x){
double x1=0;
for(int g=0;g<i+1;g++){
    if(x==0){
        x1=0;
    }
    if(x!=0){
        x1=mat[t][g]*pow(x,g)+x1;
    }
}
return x1;

最佳答案

当您调用f 函数 时,您将double mat 作为参数传递。它只传递一个 double 值作为参数而不是整个矩阵。您需要在 f 声明中将 double mat 更改为 double **mat

double f(double** mat,int i,int t,double x) 

然而,要实现这一点,您需要动态分配矩阵。

double** mat = (double**)malloc(sizeof(double*) * (i-1));
for (int l = 0; l < i-1; i++)
   mat[l] = (double*)malloc(sizeof(double) * (i+1)); 

那我就让你学习如何释放内存:)

您静态创建了 mat,因此很难将其作为参数传递。不过你也可以看看 here 如果您真的想将静态二维数组作为参数传递。

关于C 错误 : invalid types 'double[int]' for array subscript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27049177/

相关文章:

arrays - 如何获取数组的镜像(MATLAB)?

php - 如何更改查询的数组方案结果?

c - 您如何通过netcat将二进制数据回显到正在监听的C程序?

c - POSIX C 线程中上下文切换之前当前堆栈指针寄存器的值存储在哪里

php - 有没有更好的方法来创建数组键以避免丢失索引通知?

c - 从 C 中的数组打印重复项时出现冗余结果?

javascript - 在这种情况下,我应该使用大括号 {} 还是方括号 []?

c - K&R 中的 getline 函数定义

c - printf 死锁 - 如果在信号处理程序中使用 printf,则某些 printf 消息会被忽略

c - 数组中数字之间的升序排序