简体   繁体   English

传递struct成员以在c中起作用

[英]passing struct member to function in c

I have this file and input it as array of struct in c. 我有此文件,并将其输入为c中的struct数组。 However I have problem in passing the struct member to the function. 但是我在将struct成员传递给函数时遇到问题。 Error: Not even a pointer or an array value has been with the subscript in line 58. I am a newbie in c and stuck with this problem for a week. 错误:第58行的下标甚至都没有指针或数组值。我是c的新手,并且这个问题坚持了一周。

The codes: 代码:

#include <stdio.h>
#include <math.h>
#define SIZE 100

typedef struct list{
  int counter;
  int year;
  double maxrain;
  double rank;
} data;

double avg (struct list s, int count);

int main()
{
  data a[SIZE];
  struct list s;
  double sum = 0;
  int totalFile = 1;        // according to number of csv file
  int z, i;
  char fName[16];
  FILE*fpt;

  double mean;

  /* reading multiple file */
  for (z=1; z<=totalFile; z++)
  {
    sprintf(fName," ",z);
    fpt = fopen(fName,"r");

    if(fpt == NULL){
      printf("Error opening %s\n",fName);
      return(-1);
    }

    printf("---Reading from file %d---\n", z);
    sum = 0;
    i = 0;
    while(i <= SIZE && fscanf(fpt, "%d%*c%d%*c%f%*c%f", &a[i].counter, &a[i].year, &a[i].maxrain, &a[i].rank) != EOF){
      sum = sum + a[i].maxrain;
      i++;  
    }
    mean = avg(a[i].maxrain, i);
    printf("%f", mean);

    return 0;
  }
}

double avg(struct list s , int count)
{
  double ave;
  int i = 0;

  for(i=0; i<count; add += s.maxrain[i++]);
  ave = add/count;

  return ave;
}

There are several issues here to which you would have been pointed by the compiler, if you'd have told the compiler to tell you the maximunm of possible warnings. 如果您已告诉编译器告诉您可能出现的警告的最大值,则编译器将在此处指出几个问题。 For gcc the options to do so are -Wall -Wextra -pedantic . 对于gcc,这样做的选项是-Wall -Wextra -pedantic

But now for the issues en detail: 但现在就这些问题进行详细介绍:

Here 这里

sprintf(fName, " ", z);

the conversion specifier is missing. 缺少转换说明符。 The code should look like this: 该代码应如下所示:

sprintf(fName, "%d", z);

Also sprintf() is unsave as it might overflow the destination "string". sprintf()也未保存,因为它可能会溢出目标“字符串”。 Use snprintf() instead: 使用snprintf()代替:

snprintf(fName, "%d", sizeof(fName), z);

The following scan command uses %f which expected float, but has double being passed in. 下面的扫描命令使用%f ,它预期为float,但有double传入。

fscanf(fpt, "%d%*c%d%*c%f%*c%f", &a[i].counter, &a[i].year, &a[i].maxrain, &a[i].rank)

Use %lf to scan doubles: 使用%lf扫描双打:

fscanf(fpt, "%d%*c%d%*c%lf%*c%lf", &a[i].counter, &a[i].year, &a[i].maxrain, &a[i].rank)

This 这个

mean = avg(a[i].maxrain, i);

should be 应该

mean = avg(a[i], i);

And finally the declaration/definition of add is missing in avg() . 最后, avg()缺少add的声明/定义。


As a note on declaring variables to server as array index: 作为向服务器声明变量作为数组索引的注释:

Array indicies are always positive, so it does not make sense to use a signed variable for this. 数组索引始终为正,因此为此使用带符号变量没有意义。

Also it is uncertain how wide the one or the other integer types is, so it is not save to use every to address memory. 同样也不确定一个或另一种整数类型的宽度,因此使用每个地址寻址存储器都将是不节省的。 To stay on the save side when addressing array elements (and with this memory) use size_t , which is guaranteed by the C Standard to be an unsigned integere wide enough to address all of the machine's memory (and with this the maximum possible array element). 要在寻址数组元素(以及使用此内存)时保留在保存侧,请使用size_t ,C标准保证它是一个无符号整数,宽度足以寻址所有机器内存(并使用最大可能的数组元素) 。

1. Passing elements of struct to function: 1.将struct的元素传递给函数:

main() {
    struct list data = {1, 2, 3, 4};
    avg(data.counter, data.year, data.maxrain, data.rank, 5);
}

double avg(int counter, int year, double maxrain, double rank, int count) {
    //    
}

2. Passing struct to function 2.将结构传递给函数

main() {
    struct list data = {1, 2, 3, 4};
    avg(data, 5);
}

double avg(struct list s , int count) {
    //    
}

3. Passing address of struct to function with pointer 3.用指针将结构的地址传递给函数

main() {
    struct list data = {1, 2, 3, 4};
    avg(&data, 5);
}

double avg(struct list *s , int count) {
    //    
}

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

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