expected ) and ( error in c - c

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..

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.

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.

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

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:
You have misspelled a member name: gets(stud.claases); should be: gets(stud.classes);
Don't use gets(). Use fgets() instead as gets() isn unsafe and cause buffer overflow issue.
fflush(stdin); is undefined behaviour in C.

Related

Passing a structure to a function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
So I am having a problem when compiling this program, I just can't get it to work, I mean if I put the inputstudent() code inside the main(), it is much easier but I have to place the code in a function, inputstudent() and call in from the main(). I know it sounds very easy but I can't get it.
#include <stdio.h>
#include <stdlib.h>
struct student
{
char surname[50];
int age;
char oname[50];
char address[50];
};
void displaystudent();
void inputstudent();
int main(){
struct student s;
inputstudent(s);
displaystudent(s);
return 0;
}
void inputstudent(struct student s){
printf("Enter the surname: ");
scanf("%s",s.surname);
printf("Enter the other name: ");
scanf("%s",s.oname);
printf("Enter the age: ");
scanf("%d",&s.age);
printf("Enter the address: ");
scanf("%s",s.address);
}
void displaystudent(struct student s)
{
printf("Surname: %s \n",s.surname);
printf("Oname: %s \n",s.oname);
printf("Age: %d \n",s.age);
printf("Address: %s",s.address);
}
In C parameters are passed by value, so any modifications made to a parameter inside the function will be local modifications.
Lets have a look at following code snippet which is basically a very simple version of what you're trying to do:
void GetNumber(int number)
{
printf("Type a number:\n");
scanf("%d", &number); // modifies the local variable `number`?
}
...
int n = 0;
GetNumber(n);
Now what is the value of n right after the call to GetNumber?
Well it's not the number the user has typed, but it's still 0, that is the value n contained prior to the call to GetNumber.
What you need is this:
void GetNumber(int *pnumber)
{
printf("Type a number:\n");
scanf("%d", pnumber); // modifies the value pointed by the pointer pnumber
}
...
int n = 0;
GetNumber(&n); // &n is the memory address of the variable n
You need to read the chapter dealing with pointers in your C textbook.
Other less important problem
Your prototypes
void displaystudent();
void inputstudent();
don't match the corresponding functions.
You are passing your struct by value, that's why the function is not modifying it. You should change your function that is intended to modify the struct to take a struct pointer as argument:
#include <stdio.h>
#include <stdlib.h>
struct student {
char surname[50];
int age;
char oname[50];
char address[50];
};
void displaystudent(struct student s);
void inputstudent(struct student *s);
int main() {
struct student s;
inputstudent(&s);
displaystudent(s);
return 0;
}
void inputstudent(struct student *s) {
printf("Enter the surname: ");
scanf("%s", s->surname);
printf("Enter the other name: ");
scanf("%s", s->oname);
printf("Enter the age: ");
scanf("%d", &s->age);
printf("Enter the address: ");
scanf("%s", s->address);
}
void displaystudent(struct student s) {
printf("Surname: %s \n", s.surname);
printf("Oname: %s \n", s.oname);
printf("Age: %d \n", s.age);
printf("Address: %s", s.address);
}
As mentioned by Michael Walz, use pointers in order to modify structure in function calls. Moreover your function signature and definition does not match that's why compiler is complaining:
#include <stdio.h>
#include <stdlib.h>
struct student {
char surname[50];
int age;
char oname[50];
char address[50];
};
void displaystudent(struct student* pStudent);
void inputstudent(struct student* pStudent);
int main() {
struct student aStudent;
inputstudent(&aStudent);
displaystudent(&aStudent);
return 0;
}
void inputstudent(struct student* pStudent){
printf("Enter the surname: ");
scanf("%s", pStudent->surname);
printf("Enter the other name: ");
scanf("%s", pStudent->oname);
printf("Enter the age: ");
scanf("%d", &pStudent->age);
printf("Enter the address: ");
scanf("%s", pStudent->address);
}
void displaystudent(struct student* pStudent)
{
printf("Surname: %s \n", pStudent->surname);
printf("Oname: %s \n", pStudent->oname);
printf("Age: %d \n", pStudent->age);
printf("Address: %s", pStudent->address);
}
You seem to want the changes that you make to the structure variable s inside inputstudent() to be reflected back to the original variable. In that case you need to pass the address of the variable to the function instead of its value.
If you pass the value of s to the function instead of its address, a new copy of s would be made inside inputstudent() and the values would be read into this copy while the s in main() remains unchanged.
To solve this you give inputstudent() a pointer to the s in main() and make inputstudent() use this address while reading the data. In this way the changes made for the variable in inputstudent() will be reflected back to the s in main().
Call the function like
inputstudent(&s);
And to access members of a structure variable using a pointer to it, you use the -> operator instead of the . operator.
void inputstudent(struct student *s){
printf("Enter the surname: ");
scanf("%s",s->surname);
printf("Enter the other name: ");
scanf("%s",s->oname);
printf("Enter the age: ");
scanf("%d",&s->age);
printf("Enter the address: ");
scanf("%s",s->address);
}
Also, an address possibly involves white spaces in which scanf() won't do. You could use fgets() for that.
There are basically two causes for your problem.
Function declarations don't have any parameters.
The structure is passed by value instead of reference to inputstudent function.
You can solve both of them by changing the function prototypes in both declaration and definition to
void displaystudent(struct student s);
void inputstudent(struct student &s);
You can't use struct student s as parameter in inputstudent(). It is just a value copy.
You should use pointer as parameter.
As:
void inputstudent(struct student* s)
{...}
int main(){
struct student s;
inputstudent(&s);
displaystudent(s);
return 0;
}

Assigning user input values to allocated memory

The idea behind the program is to declare a data structure for student information, then have the user input information into each field of the data structure for an inputted amount of students. My issue is the program stops working when I input the first name. What is wrong? Thank you for your time.
//Inclusion of necessary header files
#include <stdio.h>
#include <stdlib.h>
//Data structure declaration
struct student{
char firstName[20];
char lastName[20];
char id[10];
char gender;
int age;
double gpa;
};
//Function prototypes
void readStudentsInformation(struct student *, int size);
void outputStudents(struct student *, int size);
double averageGPA(struct student *, int size);
void sortByLastName(struct student *, int size);
void sortByGPA(struct student *, int size);
//Entry point
int main()
{
//Variable delcaration
int size;
struct student *ptr;
//Input prompt and function
printf("How many students?\n");
scanf_s("%d", &size);
//Allocation memory for struct student times the number of students and assigning it to a struct student pointer for external function modification
ptr = (struct student*)malloc(sizeof(struct student)*size);
//readStudentsInformation Function call
readStudentsInformation(ptr, size);
//Exit sequence
return 0;
}
//This functions reads the information for all the students from the keyboard, taking the class size through the pointer method and struct student from main
void readStudentsInformation(struct student *ptr, int size)
{
//For loop controller declaration
int i;
//Function message
printf("Student Information Form\n");
//This for loop increments the "index" of each students info location where user input is stored
for(i=0;i<size;i++)
{
//Each field has it's appropriate input limit
printf("Please enter Student %d's First Name(20 characters).\n", i+1);
scanf_s("%20s", ptr[i].firstName);
printf("Please enter Student %d's Last Name(20 characters).\n", i+1);
scanf_s("%20s", ptr[i].lastName);
printf("Please enter Student %d's ID(10 characters).\n",i+1);
scanf_s("%10s", ptr[i].id);
printf("Please enter Student %d's gender(M/F).\n",i+1);
scanf_s("%c", ptr[i].gender);
printf("Please enter Student %d's age.\n",i+1);
scanf_s("%3d", ptr[i].age); //Only 3 digits can be put in at a time
printf("Please enter Student %d's GPA.\n", i+1);
scanf_s("%.1lf", ptr[i].gpa); //From the lab it can be seen that no more than one decimal place is featured, so the same is done here
}
//Exit to main
return;
}
You should do
scanf_s("%20s", ptr[i].firstName);
to read first name. ptr+i will give you student structure. You want to store data in the structure member.
Change your other scanfs as well for this.
You can use
scanf("%s",ptr[i].firstName);
and so on for the rest of the structure members in your scanf()'s.
change scanf_s to scanf and change this block as follow:
scanf("%c", &ptr[i].gender);
printf("Please enter Student %d's age.\n",i+1);
scanf("%3d", &ptr[i].age); //Only 3 digits can be put in at a time
printf("Please enter Student %d's GPA.\n", i+1);
scanf("%1lf", &ptr[i].gpa);

Changing a structure from inside of a function

Im suppose to print student details from the user into a student structure and I don't understand why when I compile with linux terminal, there is no entry or output. Please hep me, I'm new here.
This is my code:
#include <stdio.h>
#include <stdlib.h>
struct student
{
char *name;
int id;
char enroll;
};
int main()
{
struct student john;
john.name = "John Smith";
john.id = 12345678;
john.enroll = 'D';
}
void getStudent(struct student *john)
{
printf("Type the name of the student: ");
john->name = malloc(100);
fgets(john->name, 100, stdin);
printf("\nType the student number: ");
scanf("%d", &(john->id));
printf("\nType the student enrollment option (D or X): ");
scanf("%c", &(john->enroll));
return;
}
void printstudent(struct student john)
{
printf("Student name: %s\n", john.name);
printf("Student number: %d\n", john.id);
printf("Student enrollment option: %c\n", john.enroll);
return;
}
You need to call your functions from main (or from any function that needs them). Writing a function (declaring it) doesn't actually execute it.
int main()
{
foo(); // execute foo (call it)
}
void foo()
{
// do stuff
}
Your functions are functions like printf() and scanf() and have to also be called to be used.
You need to invoke the functions you have defined, from the main.
Add this statement inside the main function.
printstudent(john);

two or more data types in declaration

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
struct student
{
char name[30];
int roll_no;
char add[40];
char subject[15];
}
struct student p;
printf("Enter the name of student:\t");
scanf("%s",p.name);
printf("Enter the roll no. of student:\t");
scanf("%d",&p.roll_no);
printf("Enter the address of student:\t");
scanf("%s",p.add);
printf("Enter student's subject:\t");
scanf("%s",p.subject);
printf("\nThus the name of student is:%s \nHis/her roll no is :%d \n He/she lives at:%s \t and his subject is:%s",p.name,p.roll_no,p.add,p.subject);
getch();
}
The error message is---
13error: two or more data types in declaration of `p'
i am using code blocks
Put a ; after the structure definition...

Simple structure with dynamic memory allocation

Right after scanning the second element, the program crashes. It is not able to go to the scanning of the third element(i.e. grade). Need help, in figuring out what I am wrong wrong.
#include<stdio.h>
#include<stdlib.h>
#define NUM 2
typedef struct STUDENT
{
int ID;
char *name;
char *grade;
};
int main ()
{
int i;
struct STUDENT *s;
s = (struct STUDENT*)malloc(NUM*sizeof(struct STUDENT*));
printf("Enter Student's ID, Name and Grade:\n");
for(i=0;i<NUM;i++)
{
printf("Enter ID:\n");
scanf("%d", &(s+i)->ID);
printf("Enter Student Name:\n");
scanf("%s", (s+i)->name);
printf("Enter Grade:\n");
scanf("%s", (s+i)->grade);
printf("\n");
}
printf("\nInformation of the student's are:\n");
for(i=0;i<NUM;i++)
{
printf("Student ID = %d\n", (s+i)->ID);
printf("Student Name = %s\n", &(s+i)->name);
printf("Student grade = %c\n", &(s+i)->grade);
printf("\n");
}
return 0;
}
You do not allocate memory for grade and name in struct STUDENT. Trying to read input and write it to them produces undefined behavior.
Add the necessary mallocs, for example with a given maximum length STR_MAX:
s[i].name = malloc(STR_MAX);
s[i].grade = malloc(STR_MAX);
See the other answers for further errors.
malloc(NUM*sizeof(struct STUDENT*));
should be
malloc(NUM*sizeof(struct STUDENT));
also, the name and grade are not buffers but pointers. You may need a buffer (char array) or allocate memory for it dynamically.
typedef struct STUDENT
{
int ID;
char *name;
char *grade;
};
Here name and grade character type pointer. so when you malloc structure you also have to malloc name and grade too
struct STUDENT *s; should be declared like this:
STUDENT *s;
No need to cast void * from malloc. Change this line s = (struct STUDENT*)malloc(NUM*sizeof(struct STUDENT*)); to this:
s = malloc(NUM * sizeof(*s));
Instead of this scanf("%d", &(s+i)->ID);, the following may be easier to understand:
scanf("%d", &s[i].ID);
Same here:
printf("Student ID = %d\n", s[i].ID);
Most importantly, what Nabla said.

Resources