简体   繁体   English

在C中包含结构数组的函数

[英]Functions containing Array of Structures in C

I made this structure to store entries but when I compute the total I'm getting a garbage value.I went through it a lot of times but still could not find the mistake.I also tried initializing the total but couldn't get he required answer .Every time I compute the total it gives the same garbage value as earlier. 我做了这个结构来存储条目,但是当我计算总数时我得到了一个垃圾值。我经过了很多次但仍然找不到错误。我也尝试初始化总数但无法得到他的要求每次我计算总数时,它都会给出与之前相同的垃圾值。

#include <stdio.h>
#include <stdlib.h>
typedef struct
{
    char name[50];
    int Assignment[5];
    int Test[2];
    int Endsem;
    int Total;



}student;
void read(student s[],int n)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        printf("Enter the name of the student");
        scanf("%s",&(s[i].name));
        printf("Enter the assignment marks \n ");
        for(j=0;j<5;j++)
        {
            scanf("%d",&(s[i].Assignment[j]));
        }
        printf("\n Enter the Test marks \n ");
        for(j=0;j<2;j++)
        {
            scanf("%d",&(s[i].Test[j]));
        }
        printf("\n Enter the EndSem marks \n");
        scanf("%d",&(s[i].Endsem));
        printf("\n \n ");
    }
}
void compute(student s[],int n)
{
    int i,j,d=0,m=0;

    for(i=0;i<n;i++)
    {
        s[i].Total=0;
        for(j=0;j<5;j++)
        {
          d+=(s[i].Assignment[j]);
        }
        for(j=0;j<2;j++)
        {
           m+=(s[i].Test);
        }
        s[i].Total=(d+m+(s[i].Endsem));
        printf("\n The total is %d out of 100",(s[i].Total));
    }
}
void display(student s[],int n)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        printf("The entries are");
        printf("%s",(s[i].name));
        printf("assignment marks \n ");
        for(j=0;j<5;j++)
        {
            printf(" \n %d",(s[i].Assignment[j]));
        }
        printf("\nTest marks \n ");
        for(j=0;j<2;j++)
        {
            printf("%d \n ",(s[i].Test[j]));
        }
        printf("\n EndSem marks \n");
        printf("%d \n",(s[i].Endsem));
    }
}
void main()
{
    student s[1];

    read(s,1);
    display(s,1);
    compute(s,1);
}

How to solve this? 如何解决呢?

The problem is in 问题出在

  m+=(s[i].Test);

Test is an array, not a normal ( scalar ) variable. Test是一个数组,而不是一个普通( 标量 )变量。 It points to (or decays to) the starting address of the first element of the array. 它指向(或衰减到)数组第一个元素的起始地址。 Adding that to an int makes no sense here . 这里将其添加到int没有意义。

You might want to write 你可能想写

 m+=(s[i].Test[j]);

That said, as I already commented, you should rewrite the scanf() statements for 就是说,正如我已经评论过的,您应该为以下代码重写scanf()语句:

  • including the maximum field width to prevent buffer overfloe, like scanf("%49s",...) for an argument of array size 50 包括防止缓冲区溢出的最大字段宽度,例如对于数组大小为50的参数,如scanf("%49s",...)
  • pass the array name, instead of the address of array, as %s expects a pointer-to-char array, like scanf("%49s",s[i].name); 传递数组名称,而不是数组地址,因为%s需要一个指向字符数组的指针,例如scanf("%49s",s[i].name);

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

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