Hard time printing struct members with a function - c

I'm new to C and I'm developing this basic project while I study it, to help me fixate things... so please, bear with me if my code still looks kind of stupid.
That being said, I'm having trouble with a function to print struct members.
I've created a function to register book details and a separate one for printing said details.
If I print the details within the registerBook function, they are printed correctly.
However, when I call the method printBook, all I get is "garbage". And it's always the same characters,
The code is as follows:
#include <stdio.h>
#include <stdlib.h>
struct Books {
char title[30];
char author[20];
int book_id[10];
char subject[50];
} Books;
int main() {
struct Books Book1;
struct Books Book2;
registerBook(Book1);
printBook(Book1);
registerBook(Book2);
printBook(Book2);
int exit = 0;
while(exit == 0) {
scanf("%p", exit);
}
return 0;
}
void printBook(struct Books a){
printf("\nTitle: %s", a.title);
printf("\nAuthor: %s", a.author);
printf("\nISBN: %d", a.book_id);
printf("\nSubject: %s", a.subject);
}
void registerBook(struct Books a){
printf("\nTitle?");
scanf("%s", &a.title);
printf("\nAuthor?");
scanf("%s", &a.author);
printf("\nISBN?");
scanf("%d", &a.book_id);
printf("\nSubject?");
scanf("%s", &a.subject);
}
All I get is:
Title?one
Author?two
ISBN?3
Subject?four
Title: ç Author: ` ISBN: 6356340 Subject: Ç# Title?five
Author?six
ISBN?7
Subject?eight
Title: &Ý=w¬8wÝ=wÃÊpï Author: ISBN: 6356340 Subject:
Could someone please advise?

In the registerBook function you should pass the parameters by reference and not by value so you can keep the changes after the function ends.
#include <stdio.h>
#include <stdlib.h>
struct Books {
char title[30];
char author[20];
int book_id;
char subject[50];
} Books;
void printBook(struct Books a){
printf("\nTitle: %s", a.title);
printf("\nAuthor: %s", a.author);
printf("\nISBN: %d", a.book_id);
printf("\nSubject: %s", a.subject);
}
void registerBook(struct Books* a){
printf("\nTitle?");
scanf(" %s", a->title);
printf("\nAuthor?");
scanf(" %s", a->author);
printf("\nISBN?");
scanf(" %d", &a->book_id);
printf("\nSubject?");
scanf(" %s", a->subject);
}
int main() {
struct Books Book1;
struct Books Book2;
registerBook(&Book1);
printBook(Book1);
registerBook(&Book2);
printBook(Book2);
return 0;
}
I didn't include your exit loop as it is something that has nothing to do with your problem here.

Related

What does this error message for struct mean?

I tried to make a small registration programm dividing it in a header, a source code containing the registration function definition and the main code.
However, i get this error message:
error: expected expression before 'struct'.
What did I do wrong? Please help I'm a beginner. Here's my main code.
#include <stdio.h>
#include <stdlib.h>
#include "registration.h"
int main()
{
int choice;
puts("Press 1 to register");
scanf("%d", &choice);
if(choice==1){
registerUser(struct generalUser user);
}
}
This my header code.
struct generalUser{
char fName[15];
char lName[20];
int id;
int bDay;
int bMonth;
int bYear;
};
struct generalUser user;
void registerUser(struct generalUser user);
Here's my code for defining the function.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void registerUser(struct generalUser user){
srand(time(NULL));
puts("Insert name");
scanf("%s", user.fName);
puts("Insert last name");
scanf("%s", user.lName);
user.id=1 + rand()%9999;
printf("You ID code is %d", &user.id);
printf("Insert date of birth in the following format: dd/mm/yy");
scanf("%d/%d/%d", &user.bDay, &user.bMonth, &user.bYear);
printf("%s %s, id %d, %d/%d/%d", user.fName, user.lName, user.id, user.bDay, user.bMonth, user.bYear);
}
There are two bugs in your source:
printf("You ID code is %d", &user.id); : please remove & because what your are printing is its value not ref
In main registerUser(struct generalUser user);, please remove struct generalUser. You are calling a function having user already declared global.

Compiler giving an unexpected error for casting a pointer as a struct in calloc()

I want to create a dynamic array of pointers, each pointing to a diffrent structure, and the compiler gives me error: expected ')' before 'book'.I have tried struct * book, but it fails nevertheless. Why does it give this error and how to fix it?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char book_name[32];
char book_genre[32];
char author[32];
int page_count;
float price;
}Sbook;
typedef struct
{
char library_name[32];
Sbook * bookp;
int books_count;
}Slib;
void addexistingBooks(Slib library, Sbook book, int i);
int main()
{
Slib library;
Sbook book;
int i=0;
printf("Enter amount of books inside the library: ");
scanf("%d", &(library.books_count));
library.bookp = (Sbook * book)calloc(library.books_count,sizeof(struct book*));
addexistingBooks(library, book, i);
free(library.bookp);
return 0;
}
void addexistingBooks(Slib library, Sbook book, int i)
{
for(i=0;i<library.books_count;i++)
{
printf("Enter the name of the book: \n");
fgets(library.bookp[i].book_name,32,stdin);
printf("Enter the genre of the book: \n");
fgets(library.bookp[i].book_genre,32,stdin);
printf("Enter the author of the book: \n");
fgets(library.bookp[i].author,32,stdin);
printf("Enter the page count of the book: \n");
scanf("%d", &(library.bookp[i].page_count));
printf("Enter the price of the book: \n");
scanf("%f", &(library.bookp[i].price));
fflush(stdin);
}
}

Error initializing structures with helper function

#include <stdio.h>
int j=0;
struct student
{
int CNE;
char Nom[20];
char Prenom[20];
char Ville[20];
float Note[3];
float Moyenne;
};
void read_struct(struct student stu)
{
stu.Moyenne=0;
printf("Nom de l'etudiant:\t ");
scanf(" %s",stu.Nom);
printf("Prenom de l'etudiant:\t ");
scanf(" %s",stu.Prenom);
printf("CNE de l'etudiant:\t ");
scanf("%d",&stu.CNE);
}
int main()
{
struct student stu[10];
read_struct(stu[0]);
read_struct(stu[1]);
printf("%s \n %s \n",stu[0].Nom,stu[1].Nom);
printf("%d \n %d",stu[0].CNE,stu[1].CNE);
}
I m getting some weird output after compiling, the input from users are not saved in struct after calling them back.( sorry for my english)
Look at how this function is defined:
void read_struct(struct student stu) {
...
}
When you call this function, it passes in a copy of the struct student, so the function does its work to fill in the copy rather than the original.
You may want to have this function take in a pointer to the struct student:
void read_struct(struct student* stu) {
/* You'll need to change things here */
}
read_student(&stu[0]);
read_student(&stu[1]);
Hope this helps!

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

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

Resources