简体   繁体   中英

passing struct member to function in c

I have this file and input it as array of struct in c. However I have problem in passing the struct member to the function. 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.

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 .

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". Use snprintf() instead:

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

The following scan command uses %f which expected float, but has double being passed in.

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:

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() .


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).

1. Passing elements of struct to function:

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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