简体   繁体   English

在指向整数数组的指针中使用scanf

[英]using scanf in pointers to integer array

This program is supposed to store inputted grades of 3 students and prints the average. 该程序应存储3个学生的输入成绩并打印平均值。 However, if I ask for the average of student B, it prints the average of student A. and Student A's average is 0. I can't seem to find where I went wrong.. Please help eg Student_A = {7,7,7}, Student_B = {8,8,8}; 但是,如果我要求学生B的平均值,它会打印学生A的平均值。学生A的平均值是0。我似乎找不到我哪里出错了。请提供帮助,例如Student_A = {7,7, 7},Student_B = {8,8,8}; ave(Student_B) = 7 ave(Student_B)= 7

#include<stdio.h>

int i;
char j;
int student_A[4];
int student_B[4];
int student_C[4];
float grade_input(int student[]);
float ave(int student[]);

main(){
    printf("For Student A:\n");
    grade_input(&student_A[4]);
    printf("For Student B:\n");
    grade_input(&student_B[4]);
    printf("For Student C:\n");
    grade_input(&student_C[4]);
    do{
        printf("Whose average grade do you want to see, a ,b ,c?  ");
        getchar();
        scanf("%c", &j);

        if(j=='a'){
            printf("%.2f\n", ave(student_A));
        }
        if(j=='b'){
            printf("%.2f\n", ave(student_B));
        }
        if(j=='c'){
            printf("%.2f\n", ave(student_C));
        }
    }while(j=='a' || j=='b' || j=='c');
}

float grade_input(int student[]){
    int i;
    for(i=0; i<3; i++){
        printf("Enter grade %d: ", i+1);
        scanf("%d", &student[i]);
    }
}

float ave(int student[]){
    return (student[0] + student[1] + student[2])/3.0;
}

The problem you have is passing &student_X[4] to grade_input() . 您遇到的问题是将&student_X[4]传递给grade_input() This is just straight illegal as it is a 4 element array; 这是直接非法的,因为它是4元素数组; accessing an element with subscript 4 is not defined. 未定义访问带有下标4的元素。 Since they are defined contiguously, you end up effectively sending the pointer to student_B when you call the grade_input() function with &student_A[4] and that's exactly what you are seeing! 由于它们是连续定义的,因此当您使用&student_A[4]调用grade_input()函数时,最终可以有效地将指针发送到student_B ,这正是您所看到的!

You should instead pass a pointer to the array. 您应该改为将指针传递给数组。 I've slightly rewritten your code below to illustrate. 我在下面稍微重写了您的代码以进行说明。

#include<stdio.h>

int i;
char j;
int student_A[4];

/* ******* */
/* note that student_A is defined as a 4 element array */

int student_B[4];
int student_C[4];
float grade_input(int student[]);
float ave(int student[]);

main(){
  printf("For Student A:\n");

  /* ******* */
  /* note that student_A is defined as a 4 element array */
  /* a pointer to the array is just student_A, not &student_A[4] */
  grade_input(student_A);
  printf("For Student B:\n");
  grade_input(student_B);
  printf("For Student C:\n");
  grade_input(student_C);
  do{
    printf("Whose average grade do you want to see, a ,b ,c?  ");
    scanf("%c", &j);

    if(j=='a'){
      printf("%.2f\n", ave(student_A));
    }
    else if(j=='b'){
      printf("%.2f\n", ave(student_B));
    }
    else if(j=='c'){
      printf("%.2f\n", ave(student_C));
    }
    else
      printf ( "Enter a, b or c\n" );

  }while(j != 'q');
}

float grade_input(int student[]){
  int i;
  for(i=0; i<3; i++){
    printf("Enter grade %d: ", i+1);
    scanf("%d", &student[i]);
  }
}

float ave(int student[]){
  return (student[0] + student[1] + student[2])/3.0;
}

there is no need to do getchar(); 不需要做getchar(); before scanf("%c", &j); scanf("%c", &j); whoever you should clean the buffer with fflush(stdout); 谁应该用fflush(stdout);清理缓冲区fflush(stdout); or do 或做

char c;
while ((c = getchar()) != '\n');

so you're full code should be like: 所以你的完整代码应该是这样的:

#include<stdio.h>

int i;
char j;
int student_A[4];
int student_B[4];
int student_C[4];
float grade_input(int student[]);
float ave(int student[]);

main(){
    printf("For Student A:\n");
    grade_input(&student_A[4]);
    printf("For Student B:\n");
    grade_input(&student_B[4]);
    printf("For Student C:\n");
    grade_input(&student_C[4]);
    do{
        printf("Whose average grade do you want to see, a ,b ,c?  ");
        fflush(stdout); // THE CHANGE
        scanf("%c", &j);

        if(j=='a'){
            printf("%.2f\n", ave(student_A));
        }
        if(j=='b'){
            printf("%.2f\n", ave(student_B));
        }
        if(j=='c'){
            printf("%.2f\n", ave(student_C));
        }
    }while(j=='a' || j=='b' || j=='c');
}

float grade_input(int student[]){
    int i;
    for(i=0; i<3; i++){
        printf("Enter grade %d: ", i+1);
        scanf("%d", &student[i]);
    }
}

float ave(int student[]){
    return (student[0] + student[1] + student[2])/3.0;
}

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

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