Beginning with Structs in C - c

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

Related

C - Access a struct variable from different functions

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

Problems with C structs and int variables

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

struct in function and output in a file

Can you help me with passing elements of struct into a function (void show_info_abt_bus) for outputting my information? I don't understand how I should pass those elements.
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct info_bus_{
int number;
int begin;
int end;
char* stations;
int time_working;
}info_bus;
void input_data_abt_bus(info_bus **,int);
void show_info_abt_bus(info_bus *,int);
void my_free(info_bus **,int);
void input_data_abt_bus(info_bus **b,int n){
int i;
char buffer[128];
if(!((*b)=(info_bus *)malloc(n*sizeof(info_bus)))){
printf("Error memory\n");
exit(0);
}
for(i=0;i<n;i++){
printf("Input the number of a bus: \n");
scanf("%d",&((*b)[i].number));
printf("%d)Input when it starts to work: \n",i+1);
scanf("%d",&((*b)[i].begin));
printf("%d)Input when it finishes to work: \n",i+1);
scanf("%d",&((*b)[i].end));
printf("%d)Input its stations: \n",i+1);
scanf(" %127[^\n]%*c", buffer);
(*b)[i].stations = (char*) malloc(strlen(buffer) + 1);
strcpy((*b)[i].stations, buffer);
getchar();
printf("Input time working: \n");
scanf("%d",&((*b)[i].time_working));
}
}
void my_free(info_bus **b,int n){
int i;
for (i = 0; i < n; i++) {
free((*b)[i]);
}
free(b);
}
int main()
{
int i,n;
printf("How many buses u have: \n");
scanf("%d",&n);
info_bus *b=NULL;
input_data_abt_bus(&b,n);
show_info_bus_abt_bus(b,n);
my_free(b,n);
return 0;
}
You need to pass structure object Pass by reference in function.
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct info_bus_{
int number;
int begin;
int end;
char* stations;
int time_working;
}info_bus;
void input_data_abt_bus(info_bus **,int);
void show_info_abt_bus(info_bus *,int);
void show_info_bus_abt_bus(info_bus *b,int n){
int i;
for (i=0;i<n;i++){
printf("\n===============================================");
printf("\n[%d].the number of a bus: %d",i+1,b[i].number);
printf("\n[%d]. Begin at: %d am",i+1,b[i].begin);
printf("\n[%d]. Finishes at: %d pm",i+1,b[i].end);
printf("\n[%d]. Stations: %s",i+1,b[i].stations);
printf("\n[%d]. Time working: %d",i+1,b[i].time_working);
printf("\n===============================================\n");
}
}
void input_data_abt_bus(info_bus **b,int n){
int i;
char buffer[128];
(*b)=(info_bus *)malloc(n*sizeof(info_bus));
for(i=0;i<n;i++){
printf("Input the number of a bus: \n");
scanf("%d",&((*b)[i].number));
printf("%d)Input when it starts to work: \n",i+1);
scanf("%d",&((*b)[i].begin));
printf("%d)Input when it finishes to work: \n",i+1);
scanf("%d",&((*b)[i].end));
printf("%d)Input its stations: \n",i+1);
scanf(" %127[^\n]%*c", buffer);
(*b)[i].stations = (char*) malloc(strlen(buffer) + 1);
strcpy((*b)[i].stations, buffer);
getchar();
printf("Input time working: \n");
scanf("%d",&((*b)[i].time_working));
}
}
void my_free(info_bus **b,int n)
{
int i;
for (i = 0; i < n; i++) {
free((*b)[i].stations);
}
free((*b));
(*b)=NULL;
}
int main()
{
int i,n;
printf("How many buses u have: \n");
scanf("%d",&n);
info_bus *b=NULL;
input_data_abt_bus(&b,n);
show_info_bus_abt_bus(b,n);
my_free(&b,n)
return 0;
}

C programming, solving C2073 error in structures?

So, I'm new in this, and trying structures out... Error C2073 appeares..can someone help and give some advice?
I tried with FOR in main function to call functions "ispis" which is for printf only, and function "unos" which is for scanf so many times, how big is int "broj_knjiga". I tried to work with -> instead of . but I simply can't solve this problem(which is simple). Someone help?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void ispis(struct knjiga *pt, int broj)
{
int i;
for(i=0; i<broj; i++)
{
printf("\ID knjige: %d", &pt[i].ID_knjige);
printf("\Autor knjige: %s", &pt[i].autor);
printf("\Naslov knjige: %d", &pt[i].naslov);
}
}
void unos(struct knjiga *pt, int broj)
{
int i;
for(i=0; i<broj; i++)
{
printf("\nUnesite ID knjige: ");
scanf("%d", &pt[i].ID_knjige);
printf("\nUnesite autora knjige: ");
scanf("%d", &pt[i].autor);
printf("\nUnesite naslov knjige: ");
scanf("%d", &pt[i].naslov);
}
}
struct knjiga
{
int ID_knjige;
char autor[40];
char naslov[20];
};
int main()
{
struct knjiga *pt;
int broj_knjiga;
printf("Unesite koliko knjiga unosite: ");
scanf("%d", &broj_knjiga);
pt=(struct knjiga*)malloc(sizeof(struct knjiga)*broj_knjiga);
unos(pt, broj_knjiga);
ispis(pt, broj_knjiga);
return 0;
}
C is compiled from the top-down. Structs only exist below their declarations. Moving your struct definition above the rest of your code will fix your problem.
knjiga myStruct1; // invalid
struct knjiga
{
int ID_knjige;
char autor[40];
char naslov[20];
};
knjiga myStruct2; // valid

why is my binary update prints gibberish/garbage instead of the information that i have inserted?

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)
{...}

Resources