无效的指针添加 [英] invalid pointer addintion

查看:127
本文介绍了无效的指针添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要打印学生的结果。

im使用结构...所有变量都是在结构中定义的,而我添加了5个主题的标记然后它给了我像无效指针添加的错误,相反,我使用了多个结构变量。



我的代码是



i want to print result of a student .
i m using structure for it.. all the variables are defined in structure while i adding marks of 5 subject then it gives me error like invalid pointer addtion, rather i'd usd more than one structure varibale.

my code is

struct result
{
 char enroll[100];


 int  m1[20],m2[20],m3[20],m4[20],m5[20];

 int t[100];

 int per[100];

};
void result()
{
    struct result r,r1,r2,r3,r4,r5;
    struct record s;

    FILE *fp;

    clrscr();

    fp=fopen("stud.txt","wb");

    printf("Enter Enrollment No : ");

    gets(r.enroll);

    if(strcmp(r.enroll,s.enroll_no)==0)
    {
       printf("Enter Marks for 5 subject:");

       scanf("%d %d %d %d %d",r1.m1,r2.m2,r3.m3,r4.m4,r5.m5);

       r.t=r1.m1+r1.m2+r1.m3+r1.m4+r1.m5;

       r1.per=r.t/5;
    }

    else
    {
    printf("Record Does Not Exist :");

    printf("Press Any Key to Exit ");

    exit(0);
   }

printf("per=%d",r1.per);
printf("total=%d",r.t);
getch();



}

推荐答案

您正在添加无效的字符串。你最好用strcat来组合字符串。



但最奇怪的是:



you are adding strings that wont work. You better use strcat for combine the strings.

but most strange is:

scanf("%s %s %s %s %s",r1.m1,r2.m2,r3.m3,r4.m4,r5.m5);





对于每个字符串,一个不同结构的自己的成员?





为什么不:< br $> b $ b



For eacht string a own member of a different struct?


Why not:

scanf("%s %s %s %s %s",r1.m1,r1.m2,r.m3,r1.m4,r1.m5);





你最好使用比m1更多的描述名称。喜欢名称或描述...



and you better use more describale names than m1. Like name or description...


你不应该使用字符串存储整数。

你应该总是检查 scanf的。例如

You should't use strings for storing integers.
And you should always check the return value of scanf. e.g.
#include <stdio.h>
#define N 5

struct result
{
  int enroll;
  int m[N];
  int t;

  double per;
};

int main()
{

  struct result r;

  printf("Enter Marks for 5 subjects: ");

  if ( scanf("%d %d %d %d %d",&r.m[0],&r.m[1],&r.m[2],&r.m[3],&r.m[4]) == N)
  {
    int n;
    r.t = 0;
    for ( n=0; n<N; ++n)
      r.t += r.m[n];
    r.per = (double)r.t / N;
    printf("r.t = %d, r.per = %g\n", r.t, r.per);

  }
  else
  {// handle user input error
  }
  return 0;
}


这篇关于无效的指针添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆