my questions here always seem to be about using functions. It still confuses me! In this textbook exercise i am asked to pass a structure by value, then adjust it and pass by reference. Initially I designed the code to have everything done in main. Now I am passing by value. So I added the new function, and I figured I passed the structure correctly but I am getting an error at line
void function1(struct Inventory inv){ that tells me parameter 1 (inv) has incomplete type. please help!
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void function1(struct Inventory inv);
struct Inventory{
char name[20];
int number;
float price;
float total;
}
void main(){
items;
void function1(items);
float total = items.number*items.price;
printf("Item\tNumber\tPrice\tTotal\tAddress of number\n");
printf("%s\t%d\t%.2f\t%.2f\t%X\n\n",items.name,items.number,items.price,total,&items.number);
getch();
}
void function1(struct Inventory inv) {
printf("Enter the name of the item: ");
scanf("%s", inv.name);
printf("Enter the number of items: ");
scanf("%d", &inv.number);
printf("Enter the price of each item: ");
scanf("%f", &inv.price);
}
You have to define your struct BEFORE you use it in your function prototype.
struct Inventory{
char name[20];
int number;
float price;
float total;
}items;
void function1(struct Inventory inv);
Related
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);
}
}
I am getting errors during the compilation of this C program and is related to the declaration of the function. What is the problem here? When I declare void display(student); it shows a warning but if I change to void display(struct student st) it shows some errors.
#include<stdio.h>
void display(student);
void read_student(student);
struct student{
int roll;
char name[20],depart[20],sex,result;
float percent,marks[5],total;
};
void main(){
int n;
printf("enter the no of data you want to enter ??");
scanf("%d",&n);
struct student s[n];
for(int i=0;i<n;i++)
read_student(&s[i]);
printf("\n---------------------------------------------------Result Sheet --------------------------------------------------------------------");
printf("\nRoll No.\tName\t\tSex\tDepartment\t\tMarks\t\t\t\tTotal\tPercentage\tResult ");
printf("\n----------------------------------------------------------------------------------------------------------------------------------------");
printf("\n \t\t\t\tA\tB\tC\tD\tE\n");
printf("----------------------------------------------------------------------------------------------------------------------------------------");
for(int i=0;i<n;i++)
display(s[i]);
}
void display(struct student st){
printf("\n%2d\t%10s\t\t%c\t%10s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%4.2f\t %2.2f\t\t%c\n",st.roll,st.name,st.sex,st.depart,st.marks[0],st.marks[1],st.marks[2],st.marks[3],st.marks[4],st.total,st.percent,st.result);
}
void read_student(struct student *std){
int c=0,i;
printf("Enter the roll no:");
scanf("%d",&std->roll);
printf("enter the name:\n");
scanf("%s",std->name);
printf("enter Sex:\n");
scanf(" %c",&std->sex);
printf("Enter the department:\n");
scanf("%s",std->depart);
printf("enter the marks in 5 subjects:\n");
for(i=0;i<5;i++){
scanf("%f",&std->marks[i]);
std->total=std->total+std->marks[i];
if(std->marks[i]>=40)
c++;
}
if(c==5)
std->result='p';
else
std->result='f';
std->percent=(std->total/500)*100;
}
The compiler needs to know what struct student is, when using it elsewhere in the code, for example declaring a function with it as type of an argument/parameter. The compiler reads the source file from top to bottom. You need to declare the structure before you use it as a type in the declaration of a function:
struct student; // forward declaration of structure `student`.
void display(struct student); // ok, because `struct student` is declared before.
void read_student(struct student*); // ok, because `struct student` is declared before.
struct student{ // definition of structure `student`.
int roll;
char name[20],depart[20],sex,result;
float percent,marks[5],total;
};
Alternatively, you can define the structure before the declarations of the functions:
struct student{ // define the structure `student` before the function declarations.
int roll;
char name[20],depart[20],sex,result;
float percent,marks[5],total;
};
void display(struct student);
void read_student(struct student*);
Another way would be to put the definitions of the functions read_student and display before main(), but after the the definition of the structure student. In this way you can omit the separate declarations of the functions read_student and display and of course the forward declaration of the structure student as well:
#include<stdio.h>
struct student{
int roll;
char name[20],depart[20],sex,result;
float percent,marks[5],total;
};
void read_student(struct student *std){
int c=0,i;
printf("Enter the roll no:");
scanf("%d",&std->roll);
printf("enter the name:\n");
scanf("%s",std->name);
printf("enter Sex:\n");
scanf(" %c",&std->sex);
printf("Enter the department:\n");
scanf("%s",std->depart);
printf("enter the marks in 5 subjects:\n");
for(i=0;i<5;i++){
scanf("%f",&std->marks[i]);
std->total=std->total+std->marks[i];
if(std->marks[i]>=40)
c++;
}
if(c==5)
std->result='p';
else
std->result='f';
std->percent=(std->total/500)*100;
}
void display(struct student st){
printf("\n%2d\t%10s\t\t%c\t%10s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%4.2f\t %2.2f\t\t%c\n",st.roll,st.name,st.sex,st.depart,st.marks[0],st.marks[1],st.marks[2],st.marks[3],st.marks[4],st.total,st.percent,st.result);
}
int main(){
int n;
printf("enter the no of data you want to enter ??");
scanf("%d",&n);
struct student s[n];
for(int i=0;i<n;i++)
read_student(&s[i]);
printf("\n---------------------------------------------------Result Sheet --------------------------------------------------------------------");
printf("\nRoll No.\tName\t\tSex\tDepartment\t\tMarks\t\t\t\tTotal\tPercentage\tResult ");
printf("\n----------------------------------------------------------------------------------------------------------------------------------------");
printf("\n \t\t\t\tA\tB\tC\tD\tE\n");
printf("----------------------------------------------------------------------------------------------------------------------------------------");
for(int i=0;i<n;i++)
display(s[i]);
}
At the declaration of functions you can omit identifiers of parameters, but not the type of parameters.
It is also not permissible to use the structure student without preceding the struct keyword as you tried it with:
void display(student);
since you use the structure student by its definition:
struct student{
...
};
If you would use a typedef like for example:
typedef struct student{
...
} student;
You could omit the struct keyword, but if you use a typedef or just use the structure as it is, is a kind of controversy topic amongst the community with regards to readibility. All references to that, you can see on the answers to these questions:
typedef struct vs struct definitions
Why should we typedef a struct so often in C?
I personally recommend to keep on using the struct variant, even if it requires a little bit more typing with the keys, but makes your code a little bit clearer.
I need to add another structure to my code and I wish to know if this is possible?
Below is a snippet of what I want to do.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "math.h"
#define MAX_CARS 10
struct car {
double vel, position, desired_vel;
int index, on_network;
};
typedef struct car Car;
struct car_2 {
double vel_2, position_2, desired_Vel_2;
int index, on_network;
};
typedef struct car_2 Car_2;
You can have unlimited struct types in your code. In this instance though, as they both are identical, you can re-use your Car struct and make separate instances with it.
Car car1;
Car car2;
car1.vel = 5.0;
car2.vel = 10.0;
Yes you can use as many as you want. Example code as below:
#include <stdio.h>
struct car_dim
{
int feet;
float inch;
} card;
struct showroom_loc
{
int lid;
int pin;
} carl;
int main()
{
printf("enter the information for card struct\n");
printf("Enter feet: \n");
scanf("%d", &card.feet);
printf("Enter inch: \n");
scanf("%f", &card.inch);
printf("enter the information for car location struct\n");
printf("enter lid e\n");
scanf("%d", &carl.lid);
printf("Enter pin: \n");
scanf("%f", &carl.pin);
printf("struct card= %d\'-%.1f\"", card.feet, card.inch);
printf("\nstruct carl= %d\'-%.1f\"", carl.lid, carl.pin);
return 0;
}
You can use as many structures in your code as you like as long as their name is different.
However in your example, the members of your structures are identical, maybe you want to do something like this?
#define MAX_CARS 10
struct car {
double vel, position, desired_vel;
int index, on_network;
};
typedef struct car Car;
int main() {
// You can define as many instances of type "Car" as you want
Car car1;
Car car2;
Car car3;
// Or you can use arrays
Car cars[MAX_CARS];
cars[0].desired_vel = 100.0;
cars[1].desired_vel = 120.0;
// and so on
return 0;
}
The errors that prevent my code from compiling are:
[Error] expected identifier or '(' before '.' token
[Error] expected expression before 'books'
[Error] too few arguments to function 'fread'
I'm new to C programming and programming altogether. Although I'm used to Pascal. The errors occur in all functions except in the main and void menu. This is only a part of my code.
Please can someone help me in solving these errors this program.
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
char catergories[][15]={"Computer","Philosophy","Arts","Literature","Science","History"};
void menu(void);
void addbooks(void);
void deletebooks(void);
void updatebooks(void);
void findbooks(void);
void sellbooks(void);
void viewbooks(void);
int enterinfo();
int checkid(int);
void Password();
//list of global files that can be accessed from anywhere in program
FILE *fp,*ft,*fs;
//list of global variable
int s;
char findbook;
char password[10]={"dominique"};
typedef struct
{
int mm,dd,yy;
}OrderDate;
typedef struct
{
int id;
char name[35];
char author[35];
int quantity;
float price;
int count;
int shelf;
char *cat;
struct OrderDate *sold;
}books;
void addbooks(void) //funtion that add books
{
system("cls");
int i;
struct books;
printf("\n ******************************************************\n");
printf("\n \t\t ADD A BOOK");
printf("\n ******************************************************\n\n");
printf("\n SELECT THE CATERGORY OF THE BOOK:");
printf("\n 1. Computer Science, Information & General Works");
printf("\n 2. Philosophy, Psychology and Religion");
printf("\n 3. Arts and Recreation");
printf("\n 4. Literature");
printf("\n 5. Science");
printf("\n 6. History and Geography");
printf("\n 7. Back to main menu\n");
printf("\n Enter your choice:");
scanf("%d",&s);
if(s==7)
{
menu() ;
}
system("cls");
fp=fopen("BookRecords.txt","ab+");
if(enterinfo()==1)
{
books.cat=catergories[s-1]; //the error that exists here states expected identifier or '(' before '.' token
fseek(fp,0,SEEK_END);
fwrite(&books,sizeof(books),1,fp); //the error that exists here states expected expression before 'books'
fclose(fp);
printf("\n The book's information has been saved sucessfully");
printf("\n Would you like to save more information? (Y/N):");
if(getch()=='n')
{
menu();
}
else
system("cls");
addbooks();
}
}
struct books;
books is your type name. Like int or char. Thats why books.id==d won't work. You have to declare variable that is type of books. For example:
books BooksInMyShop;
Now you can use your variable in code ex.:
if(BooksInMyShop.id == currentId)
PS1: When You define:
typedef struct
{
(...)
}books;
You dont need to declare variable by struct books BooksInMyShop; simple books BooksInMyShop; will work now.
PS2: I can see in your code lots of missconceptions. For example "variable" books in every function in not shared beetween them. I guess You have to pass one instance of Your bookstore to every function or have one static global instance.
Error after providing the input value of ID. But working when values directly assigned. Compiled Successfully.
#include<stdio.h>
#include <string.h>
typedef struct student
{
char name[20];
int id;
int mob;
} stu;
void printstudent(stu *stud);
void main()
{
stu s1;
strcpy(s1.name,"name");
printf("Enter Student id");
scanf("%d",s1.id);
//s1.id=1;
printf("Enter Student Mob no");
scanf("%d",s1.mob);
//s1.mob=9911;
printstudent(&s1);
}
void printstudent(stu *stud)
{
printf("\n%d",stud->id);
printf("\n%s",stud->name);
printf("\n%d",stud->mob);
}
Error after providing the input value of ID. But working when values directly assigned.
s1.id and s1.mob are not pointers to the int, you should use &s1.id and &s1.mob