预期的)和(c中的错误

时间:2013-08-07 08:45:36

标签: c pointers structure

编译时我遇到错误:预期)和(在c中用于以下程序:

    #include<stdio.h>
    #include<conio.h>
    struct student
    {
        char name[20];
        int rollno;
        int age;
        char classes[10];
    };
    void printdata(struct student &sob); //getting error in this line
    void main()
    {
        struct student stud;
        clrscr();
        printf("enter student details:");
        printf("\nenter student name:"); fflush(stdin);
        gets(stud.name);
        printf("\nenter age:");
        scanf("%d",&stud.age);
        printf("\nenter rollno:");
        scanf("%d",&stud.rollno);
        printf("\nenter class of student:"); fflush(stdin);
        gets(stud.claases);
        printdata( &stud);
        getch();
    }
    void printdata(struct student &sob) //getting error in this line
    {
        struct student *ptr;
        ptr=sob;
        printf("student details are as follows:");
        printf("\nstudent's name:"); fflush(stdout);
        puts(ptr->name);
        printf("\n student' age:%d",ptr->age);
        printf("\n student's roll no:%d",ptr->rollno);
        printf("\n student's class:"); fflush(stdout);
        puts(ptr->classes);
    }

这是我已经宣布结构学生然后为什么它给我错误(和)两行...

4 个答案:

答案 0 :(得分:1)

  • struct student &无效C.它似乎是C ++代码。
  • void main()无效C(除非该程序是一个独立的程序,但这个程序显然不是这样)。
  • 根据C11标准,已从C语言中删除了gets()函数。
  • fflush(stdin)是未定义的行为。

无关,您的代码难以阅读。养成在不同函数和声明之间添加一些空行的习惯。

无关,似乎您正在使用Turbo C for DOS或同样糟糕且非标准的东西。不要使用这样的旧垃圾编译器,使用错误的编译器是所有这些问题的一个来源。

答案 1 :(得分:1)

你写了一个错字:

gets(std.claases); // it's std.classes

printdata()param应该是“struct student * sob”。

此解决方案应该有效:

#include <stdio.h>

struct student {
    char name[20];
    int rollno;
    int age;
    char classes[10];
};

void printdata(struct student *sob);

int main(void) {
    struct student stud;
    printf("enter student details:");
    printf("\nenter student name:");
    fflush(stdin);
    gets(stud.name);
    printf("\nenter age:");
    scanf("%d", &stud.age);
    printf("\nenter rollno:");
    scanf("%d", &stud.rollno);
    printf("\nenter class of student:");
    fflush(stdin);
    gets(stud.classes);
    printdata(&stud);
    return 0;
}

void printdata(struct student *sob)
{
    struct student *ptr;
    ptr = sob;
    printf("student details are as follows:");
    printf("\nstudent's name:");
    fflush(stdout);
    puts(ptr->name);
    printf("\n student' age:%d", ptr->age);
    printf("\n student's roll no:%d", ptr->rollno);
    printf("\n student's class:");
    fflush(stdout);
    puts(ptr->classes);
}
顺便说一下,主函数必须返回一个整数,它是一个标准。

答案 2 :(得分:0)

您的函数printData应接受指针struct student *ob而不是struct student &ob

答案 3 :(得分:0)

C中没有引用(就像在C ++中一样)。更改函数原型和defintion以将指针作为参数。

更改

void printdata(struct student &sob); //getting error in this line

void printdata(struct student *sob); //getting error in this line

并更改

void printdata(struct student &sob) //getting error in this line

void printdata(struct student *sob) //getting error in this line

其他问题包括:

  1. 您拼错了会员名称:gets(stud.claases);应该是:gets(stud.classes);
  2. 请勿使用gets()。请使用fgets()代替gets()不安全并导致缓冲区溢出问题。
  3. fflush(stdin);是C中未定义的行为。