I'm trying to declare a variable (struct typed) in a function, and manipulate it (read/write) from other functions. However, when I try to use that variable in any function that is not where I declared it, it contains only garbage.
This is my code:
#include <stdio.h>
#include <stdlib.h>
typedef struct
char name[25];
int roll;
float marks;
}Student;
void getInfo(Student student);
void display(Student student);
int main(int argc, char *argv[]) {
Student student;
getInfo(student);
display(student);
return 0;
}
void getInfo(Student student){
printf("Enter student's name: \n");
scanf("%s", &student.name);
printf("Enter student's roll: \n");
scanf("%d", &student.roll);
printf("Enter student's grade: \n");
scanf("%f", &student.marks);
}
void display(Student student){
printf("NAME: %s\n", student.name);
printf("ROLL: %d\n", student.roll);
printf("GRADE: %.2f\n", student.marks);
}
You should pass your struct by reference ( & operator )
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char name[25];
int roll;
float marks;
} Student;
void getInfo(Student *student);
void display(Student *student);
int main(int argc, char *argv[]) {
Student student;
getInfo( &student );
display( &student );
return 0;
}
void getInfo(Student *student){
printf("Enter student's name:");
scanf("%s", student->name);
printf("Enter student's roll:");
scanf("%d", &student->roll);
printf("Enter student's grade:");
scanf("%f", &student->marks);
}
void display(Student *student){
printf("NAME: %s\n", student->name);
printf("ROLL: %d\n", student->roll);
printf("GRADE: %.2f\n", student->marks);
}
Demo : https://repl.it/repls/LowNonstopWeb
Related
This is the output I need. This is what I've got so far. I've only been able to figure out how to add 1 subject, but I need to add 2 more like the picture. I tried to save directly into the array but it didn't work. So i used strcpy.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char subjectID[10];
char subjectName[10];
float marks;
}Subject;
typedef struct {
char Stu_ID[10];
char Stu_Name[30];
Subject subjects;
}Student;
int main()
{
Student a[3];
Student b;
int i;
strcpy(b.Stu_ID, "S100011");
strcpy(b.Stu_Name, "James");
strcpy(b.subjects.subjectID, "TOS10005");
strcpy(b.subjects.subjectName, "Programming");
b.subjects.marks=50;
printf("Student details \n");
printf("ID : %s\t Name : %s\n\n", b.Stu_ID, b.Stu_Name);
printf("Subject ID\t Name\t\t Marks\n");
printf("%s\t %s\t\t %.2f\t \n", b.subjects.subjectID, b.subjects.subjectName, b.subjects.marks);
}
#include <stdio.h>
#include "struct.h"
#define NUM 3
struct Student
{
char name[20];
int age;
};
int main(void)
{
struct Student s_array[NUM];
for(int i=0;i<NUM;i++)
{
printf("name: ");
scanf("%s",s_array[i].name);
printf("age: ");
scanf("%i",s_array[i].age);
}
for(int i=0;i<NUM;i++)
{
printf("%s is %i years old",s_array[i].name,s_array[i].age);
}
return 0;
}
I don't know what's the problem I declared an array of structs of type student and used a for loop to initialise their fields, but when I type in the age it gives me segmentation fault. why is that??
struct.c:17:20: warning: format specifies type 'int *' but the argument has type 'int' [-Wformat]
scanf("%d",s_array[i].age);
The int in the struct is an int, not a pointer to an int. Arrays can be assigned directly to pointers, but on other types, you need to apply the addressof (&) operator. Instead of saying "s_array[i].age", say "&(s_array[i].age)".
I worked out this code for you
#include < stdio.h>
#include < stdlib.h>
#define NUM 3
struct Student
{
char name[20];
int age;
};
int main(void)
{
struct Student s_array[NUM];
for(int i=0;i<NUM;i++)
{
printf("name: ");
scanf("%s",&s_array[i].name);
printf("age: ");
scanf("%d",&s_array[i].age);
}
for(int i=0;i<NUM;i++)
{
printf("%s is %d years old",s_array[i].name,s_array[i].age);
}
return 0;
}
#include<stdio.h>
//#include "struct.h>
#define NUM 3
struct Student
{
char name[20];
int age;
};
int main(void)
{
struct Student s_array[NUM];
for(int i=0;i<NUM;i++)
{
printf("name: ");
scanf("%s",&s_array[i].name);
printf("age: ");
scanf("%d",&s_array[i].age);//You sud use "&"operator to take input
}
for(int i=0;i<NUM;i++)
{
printf("%s is %d years old",s_array[i].name,s_array[i].age);
}
return 0;
}
Every time I try to compile the following code I get a boatload of errors.
Does anyone see any obvious things that I overlooked with my C program. Any help would be appreciated.
The header file structures.h is also attached.
This is the structures.cc file
#include <stdio.h>
#include <string.h>
#include "structures.h"
void getOnePerson(person_t *p) {
char *name;
char *ssn;
int *year;
printf("Enter full name: ");
scanf("%s", &name);
p.name = &name;
printf("\nEnter ssn: ");
scanf("%s", &ssn);
p.ssn = &ssn;
printf("\nEnter year of birth: ");
scanf("%d", year);
p.yearOfBirth = &year;
}
void printOnePerson(person_t t1) {
char *name = t1.name;
char *ssn = t1.ssn;
int year = t1.yearOfBirth;
printf("%s", &name);
printf(":");
printf("%s", &ssn);
printf(":");
printf("%p", &year);
printf("\n");
}
void getPeople(person_t peoples[], int people) {
for (int a = 0; a < people; a++) {
getOnePerson(peoples[a]);
}
}
void printPeople(person_t peoples[], int people) {
for (int p = 0; p < people; p++) {
peoples[p];
}
}
This is the structures.h file
#include <stdio.h>
#include <string.h>
#define NAME_SIZE 80
#define SSN_SIZE 13
#define NUM_PEOPLE 10
typedef struct{
char name[NAME_SIZE];
char ssn[SSN_SIZE];
int yearOfBirth;
}person_t;
void getOnePerson(struct person_t *p);
void printOnePerson(struct person_t p);
void getPeople(struct person_t peoples[], int people);
void printPeople(struct person_t peoples[], int people);
Your function declarations in the header file are incorrect.
You make reference to one or more parameters of type struct person_t, but there is no such type. There is a type named person_t, so change the function prototypes to use that.
void getOnePerson(person_t *p);
void printOnePerson(person_t p);
void getPeople(person_t peoples[], int people);
void printPeople(person_t peoples[], int people);
Your get and print functions also have several issues. In both cases, you're using intermediate variables to read/write values which aren't necessary:
void getOnePerson(person_t *p) {
printf("Enter full name: ");
scanf("%s", p->name);
printf("\nEnter ssn: ");
scanf("%s", p->ssn);
printf("\nEnter year of birth: ");
scanf("%d", &p->yearOfBirth);
}
void printOnePerson(person_t t1) {
printf("%s", t1.name);
printf(":");
printf("%s", t1.ssn);
printf(":");
printf("%d", t1.yearOfBirth);
printf("\n");
}
i'm trying to build a struct of a student that contain name,5 grades,and average. then i want to scan 5 students and write their info on a binary file and after that i want to update 1 of the students grade...
i guess the way that i call the function update or the way that i open the file is wrong...
hope you can help me..
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10
#define G 5
#define S 1
typedef struct {
char name[N];
int grade[G];
double average;
}student;
double aver(int a[G]);
void scan_student(student *student);
void print_student(student *student);
void update_grades(FILE *info);
void scan_student(student *student)
{
int j;
flushall();
printf("Enter the student's name\n");
gets(student->name);
for(j=0;j<G;j++)
{
printf("Enter the student's grade number --> %d\n",j);
scanf("%d",&student->grade[j]);
}
student->average=aver(student->grade);
}
void print_student(student *STUD)
{
int j;
printf("this is the details of the student: \n");
puts(STUD->name);
for (j=0;j<G;j++)
{
printf("grade number --> %d\n",j);
printf("%d\n",STUD->grade[j]);
}
printf("this is %s's average:\t\n%lf\n",STUD->name,STUD->average);
}
double aver(int a[G])
{
double AVER;
int sum=0,i;
for (i=0;i<G;i++)
sum+=a[i];
AVER=sum/G;
return AVER;
}
void update_grades()
{
int size,i,boolean=0;
char st[N];
FILE* info;
student student;
size=sizeof(student);
info=fopen("My Grades.txt","rb");
printf("Enter the name of the student that you want to change he's details...\n");
flushall();
gets(st);
rewind(info);
do{
fseek(info,size,SEEK_CUR);
fread(&student,size,1,info);
if(strcmp(student.name,st))
{
boolean=1;
printf("this is the selected student\n");
print_student(&student);
printf("\n\n wich gread would you like to change? choose from 1 to 5 :\n");
scanf("%d",i);
printf("insert new grade:\n");
scanf("%d",student.grade[i-1]);
printf("this is the student details after the changes:\n\n");
print_student(&student);
fseek(info,(-1)*size,SEEK_CUR);
fwrite(&student, size,1,info);
}
}while((!feof(info))&&(!boolean));
if(!boolean)
printf("\n\n there is no such student named %s....\n",st);
fclose(info);
}
void main()
{
int i,j,size;
char a;
student STUD;
FILE *txt;
txt=fopen("My Grades.txt","wb");
do{
size=sizeof(STUD);
scan_student(&STUD);
print_student(&STUD);
fwrite( &STUD,size,1,txt);
printf("would you like to add another student? Y/N\n");
do{
flushall();
a=getchar();
}while ((a!='Y')&&(a!='N'));
}while(a=='Y');
fclose(txt);
update_grades();
}
this is the error that i get...
https://www.dropbox.com/s/pc9yz3a1w1cbcm1/example%20error.jpg
ok guys..so this is the fixed code.. but still same problem..
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10
#define G 5
#define S 1
typedef struct {
char name[N];
int grade[G];
double average;
}student;
double aver(int a[G]);
void scan_student(student *student);
void print_student(student *student);
void update_grades(FILE *info);
void scan_student(student *st)
{
int j;
flushall();
printf("Enter the student's name\n");
gets(st->name);
for(j=0;j<G;j++)
{
printf("Enter the student's grade number --> %d\n",j);
scanf("%d",&st->grade[j]);
}
st->average=aver(st->grade);
}
void print_student(student *STUD)
{
int j;
printf("this is the details of the student: \n");
puts(STUD->name);
for (j=0;j<G;j++)
{
printf("grade number --> %d\n",j);
printf("%d\n",STUD->grade[j]);
}
printf("this is %s's average:\t\n%lf\n",STUD->name,STUD->average);
}
double aver(int a[G])
{
double AVER,sum=0;
int i;
for (i=0;i<G;i++)
sum+=a[i];
AVER=sum/G;
return AVER;
}
void update_grades()
{
int size,i,boolean=0;
char st[N];
FILE* info;
student STUDENT;
size=sizeof(student);
info=fopen("My Grades.txt","rb");
printf("Enter the name of the student that you want to change he's details...\n");
flushall();
gets(st);
rewind(info);
do{
fseek(info,size,SEEK_CUR);
fread(&STUDENT,size,1,info);
if(strcmp(STUDENT.name,st))
{
boolean=1;
printf("this is the selected student\n");
print_student(&STUDENT);
printf("\n\n wich gread would you like to change? choose from 1 to 5 :\n");
scanf("%d",&i);
printf("insert new grade:\n");
scanf("%d",&STUDENT.grade[i-1]);
printf("this is the student details after the changes:\n\n");
print_student(&STUDENT);
fseek(info,(-1)*size,SEEK_CUR);
fwrite(&STUDENT, size,1,info);
}
}while((!feof(info))&&(!boolean));
if(!boolean)
printf("\n\n there is no such student named %s....\n",st);
fclose(info);
}
void main()
{
int i,j,size;
char a;
student STUD;
FILE *txt;
txt=fopen("My Grades.txt","wb");
do{
size=sizeof(STUD);
scan_student(&STUD);
print_student(&STUD);
fwrite( &STUD,size,1,txt);
printf("would you like to add another student? Y/N\n");
do{
flushall();
a=getchar();
}while ((a!='Y')&&(a!='N'));
}while(a=='Y');
fclose(txt);
update_grades();
}
ok so when i add only 1 student i get this error like in the link that i shared..
And if I scan more than one student .. And choose a student from the file (not the first one)...
Then it updates the student that was before him on the list...
Error could be on declaring like this: void scan_student(student *student);. It could be void scan_student(student *STUD); Also change:
void scan_student(student *student)
{
...
}
as
void scan_student(student *STUD)
{...}
I am trying to write simple program to collect data for certain number of students and output it in the end. After I enter data for one student, my program crashes.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Student Student;
struct Student{
char name[20];
char lastname[20];
int age;
};
main() {
int i;
int n;
scanf("%d",&n);
Student *pStudents = NULL;
pStudents = (Student*)malloc(n*sizeof(Student));
for(i=0;i<n;i++) {
printf("Enter the students name: \n");
scanf("%s",(pStudents+i)->name);
printf("Enter lastname: \n");
scanf("%s",(pStudents+i)->lastname);
printf("Enter age: \n");
scanf("%d",(pStudents+i)->age);
}
for(i=0;i<n;i++) {
printf("%s",(pStudents+i)->name);
printf("%s",(pStudents+i)->lastname);
printf("%d",(pStudents+i)->age);
}
}
Thanks in advance.
scanf("%d",(pStudents+i)->age);
the argument of scanf must be of a pointer type.
Change (pStudents+i)->age to &(pStudents+i)->age.