简体   繁体   English

VSCode 不显示所有警告/错误

[英]VSCode doesn't show all warnings/errors

I have this code:我有这段代码:

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

FILE *openFile(char const * , char const *);
int readDim(FILE *);
double **allocMatrix(int , int );
void printMatrix(int , int , double **);
void readMatrix(FILE *, int , int , double **);
void mediana(int , int , double **);
void printVect(int , double *);

FILE* openFile(char const* file_name, char const* mode)
{
  FILE* fp = fopen(file_name, mode);
  printf("File name: %s\n", file_name);
  if (fp == NULL)
  {
    perror(file_name); 
    exit(EXIT_FAILURE); 
  } 
  else 
    printf("File read correctly\n");

  return fp; 
}

int main()
{
    FILE *fp=openFile("matrice.txt", "r");
    int nrows=readDim(fp), ncols=readDim(fp);
    printf("Rows: %d\tColumns: %d\n", nrows, ncols);
    double **matrix=allocMatrix(nrows, ncols);
    readMatrix(fp,nrows, ncols, matrix);
    printMatrix(nrows, ncols, matrix);
    mediana(nrows, ncols, matrix);
    return 0;
}

void mediana(int nrows, int ncols, double **matrix)
{
    int i, col=0;
    double temp;
    double *row=calloc(ncols, sizeof(double));
    for(i=0; i<ncols; i++)
    {
        row[i]=matrix[i][col];
    }
    printVect(ncols, row);
  /*  for(i=0; i<ncols; i++)
    {
        if(row[i]>row[i+1])
        {
            row[i]=temp;
            row[i]=row[i+1];
            row[i+1]=temp;
        }
    }    
    printVect(ncols, row);*/

}
void printVect(int ncols, double *row)
{
    int i;
    printf("Vector:\n");
    for(i=0; i<ncols; i++)
    {
        printf("%lf", row[i]);
    }
    
}
void readMatrix(FILE *fp, int nrows, int ncols, double **matrix)
{
    int i, j;
    for(i=0; i<nrows; i++)
    {
        for(j=0; j<ncols; j++)
        {
            fscanf(fp, "%lf", &matrix[i][j]);
        }
    }
}

void printMatrix(int nrows, int ncols, double **matrix)
{
    int i, j;
    printf("Matrix:\n");
    for(i=0; i<nrows; i++)
    {
        for(j=0; j<ncols; j++)
        {
            printf("%.4lf\t", matrix[i][j]);
        }
        printf("\n");
    }
}
double **allocMatrix(int nrows, int ncols)
{
    int i;
    double **matrix;
    matrix=calloc(nrows, sizeof(double));

    for(i=0; i<nrows; i++)
    {
        matrix[i]=calloc(ncols, sizeof(double));
    }

    if(matrix==NULL)
    {
        perror("matrix");
    }
    else
    printf("Memory allocated correctly\n");
    return matrix;
}

int readDim(FILE *fp)
{
    int num;
    fscanf(fp, "%d", &num);
    return num;
}

I am sure there is some sort of error because the output i get from VSCode is:我确定存在某种错误,因为我从 VSCode 获得的 output 是:

File name: matrice.txt
File read correctly
Rows: 6 Columns: 8
Memory allocated correctly
Matrix:
0.8147  0.2785  0.9572  0.7922  0.6787  0.7060  0.6948  0.7655
0.9058  0.5469  0.4854  0.9595  0.7577  0.0318  0.3171  0.7952
0.1270  0.9575  0.8003  0.6557  0.7431  0.2769  0.9502  0.1869
0.9134  0.9649  0.1419  0.0357  0.3922  0.0462  0.0344  0.4898
0.6324  0.1576  0.4218  0.8491  0.6555  0.0971  0.4387  0.4456
0.0975  0.9706  0.9157  0.9340  0.1712  0.8235  0.3816  0.6463

TL;DR长话短说

Everything is fine until the program has to execute the "mediana" function, I think the error is row[i]=matrix[i][col];一切都很好,直到程序必须执行“mediana”function,我认为错误是row[i]=matrix[i][col]; but the compiler doensn't show any error or warning, what should I do?但是编译器没有显示任何错误或警告,我该怎么办?

Input file:输入文件:

6 8
0.8147    0.2785    0.9572    0.7922    0.6787    0.7060    0.6948    0.7655
0.9058    0.5469    0.4854    0.9595    0.7577    0.0318    0.3171    0.7952
0.1270    0.9575    0.8003    0.6557    0.7431    0.2769    0.9502    0.1869
0.9134    0.9649    0.1419    0.0357    0.3922    0.0462    0.0344    0.4898
0.6324    0.1576    0.4218    0.8491    0.6555    0.0971    0.4387    0.4456
0.0975    0.9706    0.9157    0.9340    0.1712    0.8235    0.3816    0.6463

The problem is here:问题在这里:

row[i] = matrix[i][col];

which should rather be:应该是:

row[i] = matrix[col][i];

You mixed up something.你混淆了一些东西。

You also should free the memory at the end of mediana with:您还应该在 mediana 末尾mediana

free(row);

BTW if you run your code with your debugger (don't ask me how to do this on your platform), your program would most likely have crashed into the debugger showing you the exact line where the crash happended.顺便说一句,如果您使用调试器运行您的代码(不要问我如何在您的平台上执行此操作),您的程序很可能会崩溃到调试器中,向您显示崩溃发生的确切行。

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

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