-2

while compiling i am having an error: expected ) and ( in c for the following program:

    #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);
    }

it is that i have already declared the structure student then why is it giving me the error ( and ) in two lines..

qaphla
  • 4,707
  • 3
  • 20
  • 31
gazer
  • 1
  • 1
  • That is not C, looks more like C++, there are no references in C. Also, check your spelling among other errors, the compiler should have told you about these. – Nobilis Aug 07 '13 at 08:46
  • using printf and scanf then how could it be c++..and so sorry for my spelling mistakes but i am getting these two errors and no warning..!! – gazer Aug 07 '13 at 08:50
  • And what kind of compiler are you using? Mine says `error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token` which makes it much more clear what the error actually is. – Mr Lister Aug 07 '13 at 08:50
  • "using printf and scanf then how could it be c++" Ehm, how to say this tactfully... – Mr Lister Aug 07 '13 at 08:51
  • @shivaniits This `struct student &sob` the `&` is used to declare something as a reference in C++. In C it only exists as the `address-of` operator and it's used for example when you pass arguments to a function expecting a pointer and not in function signatures. – Nobilis Aug 07 '13 at 08:51
  • @shivaniits please don't use `fflush` on `stdin` it leads to [undefined behaviour](http://stackoverflow.com/questions/2979209/using-fflushstdin). – Nobilis Aug 07 '13 at 08:53
  • ... and if it did do what you wanted it to do, that would prevent the end user from being able to pipe input into the program. – Mr Lister Aug 07 '13 at 08:56
  • oh yes it is c++ i am so sorry for my dumbness..the compiler is turbo c++ version 3.0 boroland..and yes it is not giving any spelling mistakes though.. – gazer Aug 07 '13 at 08:59

4 Answers4

1
  • struct student & is not valid C. It appears to be C++ code.
  • void main() is not valid C (unless the program is a free standing one, which this one clearly is not).
  • The gets() function has been removed from the C language as per the C11 standard.
  • fflush(stdin) is undefined behavior.

Unrelated, your code is difficult to read. Make a habit of adding some empty lines between different functions and declarations.

Unrelated, it appears that you are using Turbo C for DOS or something equally bad and non-standard. Don't use such old crap compilers, using a bad compiler is one source for all these problems.

Lundin
  • 195,001
  • 40
  • 254
  • 396
1

You made a typo :

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

And the printdata() param should be "struct student *sob".

This solution should works :

#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);
}

BTW, the main function must return an integer, it's a standard.

Junior Dussouillez
  • 2,327
  • 3
  • 30
  • 39
0

Your function printData should accept a pointer struct student *ob not struct student &ob.

Uchia Itachi
  • 5,287
  • 2
  • 23
  • 26
0

There's no reference in C (like in C++). Change the function prototype and defintion to take pointers as argument.

Change

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

to

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

and change

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

to

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

Other problems are:

  1. You have misspelled a member name: gets(stud.claases); should be: gets(stud.classes);
  2. Don't use gets(). Use fgets() instead as gets() isn unsafe and cause buffer overflow issue.
  3. fflush(stdin); is undefined behaviour in C.
P.P
  • 117,907
  • 20
  • 175
  • 238