I'm trying to print the entered values from struct. Program is able to get all input values but after show() method call it does jumps out of the program in exit without displaying my entered values.
I'm not getting any error or warning at all.
Code:
#include<stdio.h>
#include<string.h>
struct Book{
char *title;
short price;
};
void show(struct Book b[],const short n){
short i;
printf("\nBOOK DETAILS");
for(i=0;i<n;i++){
printf("\nRecord no %d / %d",i+1,n); //exits after displaying this line
printf("\nTitle : %s \nPrice: %d",b[i].title, b[i].price);
}
}
void get(struct Book b[],const short n){
short i;
for(i=0;i<n;i++){
printf("\nRecord no %d / %d",i+1,n);
printf("\nEnter book title & price: ");
scanf("%s %d",&b[i].title, &b[i].price);
}
show(b,n);
}
int main()
{
struct Book b[3];
get(b,3);
return 0;
}
Call sequence is like main() --> get() -->show(). I'm using Dev cpp compiler with .c extension of my code file.
title member of struct Book is just a pointer to char *, so when you define a struct Book structure, no memory has been allocated for its title.
Either define title as an array or allocate memory for it.
Related
As part of a larger project, I am trying to write a function that will allocate enough memory for a struct variable and assign values to its member variables after scanning them from the keyboard. In other words, I am trying to create a sort of a constructor function. The reason for this is because I think that's the best way to create a database-like program in c, where all the student_t variables will be stored in a student_t* array called classroom.
#include <stdio.h>
#include <stdlib.h>
#define N 10
#define STRLIM 50
typedef struct student{
char* name;
int age;
}student_t;
student_t* init_student(student_t* s);
int i=0;//its the array counter
student_t* classroom[N];
int main(int argc, char const *argv[]){
while(i<N){
// classroom[i]=(student*)malloc(sizeof(student));
init_student(classroom[i]);
classroom[i]->age=i;
printf("The age of student #%d is : %d \n",i,classroom[i]->age);
printf("the name of student #%d is : %s",i,classroom[i]->name);
i++;
}
printf("the size of the classroom is : %ld",sizeof(classroom));
return 0;
}//end of main()
student_t* init_student(student_t* s){
s=(student_t*)malloc(sizeof(student_t));
s->name=(char*)malloc(sizeof(char)*STRLIM);
fflush(stdin);
fgets(s->name,STRLIM,stdin);
return s;
}
Gdb ouput shown in the attached image here.
Please check out my repo.
I am guessing something about my build and datatypes is off .Thanks in advance.
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.
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.
I'm having problems inputting values into this structure template, please help.
I've got to use char* for those attributes. so it doesn't give an error while inputting data but crashes when its time to display.
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
struct Date
{
int day;
char *month;
int year;
}date;
struct sports_team
{
char *name;
char *city;
int no_of_players;
struct Date creation_date;
};
void main()
{
struct sports_team *my_team;
my_team = (struct sports_team*)malloc(sizeof(struct sports_team)*2);
printf("fill information for teams:\n");
for(int i=0;i<2;i++)
{
printf("\nenter team name:");
scanf("%s",&my_team[i].name);
printf("\nenter team city:");
scanf("%s",&my_team[i].city);
printf("\nenter no. of players:");
scanf("%d",&my_team[i].no_of_players);
printf("\nenter creation day:");
scanf("%d",&(my_team[i].creation_date.day));
printf("\nenter creation month:");
scanf("%s",&(my_team[i].creation_date.month));
printf("\nenter creation year:");
scanf("%d",&(my_team[i].creation_date.year));
printf("\n\n");
}
printf("\n information for teams:\n");
for(int i=0;i<2;i++)
{
printf("%s ",my_team[i].name);
printf("%s ",my_team[i].city);
printf("%d ",my_team[i].no_of_players);
printf("%d ",my_team[i].creation_date.day);
printf("%s ",my_team[i].creation_date.month);
printf("%d ",my_team[i].creation_date.year);
printf("\n\n");
}
free(my_team);
}
you're allocating the structures all right but you're forgetting to allocate memory for char * members: you're writing into uninitialized memory when doing scanf (not to mention that you're passing the address of the pointer, not the pointer itself: don't use & when scanning strings).
For simplicity's sake, you could just put fixed buffer lengths in your structures:
struct Date
{
int day;
char month[20];
int year;
}date;
struct sports_team
{
char name[100];
char city[100];
int no_of_players;
struct Date creation_date;
};
and limit size when scanning (don't use & when scanning strings BTW)
scanf("%99s",my_team[i].name);
scanf("%99s",my_team[i].city);
scanf("%19s",my_team[i].creation_date.month);
Use getchar() instead of scanf().
I've got this structure, a simple one that holds student name and marks. When I'm trying to read user input into the name(char array), I get a warning indicating something on the lines of :
format %s expects char *, but has char*[20]
I know this is because char arrays cannot be assigned in C, so strcpy has to be used. This question on SO has a good reasoning. However,how do I fix the warning in my program? Don't think I can use strcpy here.
#include <stdio.h>
typedef struct _student
{
char name[20];
unsigned int marks;
} student;
void read_list(student list[], int SIZE);
void print_list(student list[], int SIZE);
int main()
{
const int SIZE=3;
student list[SIZE];
//function to enter student info.
read_list(list, SIZE);
//function to print student info
print_list(list, SIZE);
return 0;
}
void read_list(student list[], int SIZE)
{
int i;
char nm[20];
for (i=0;i<SIZE;i++)
{
printf("\n Please enter name for student %d\n", i);
scanf("%s",&list[i].name);
printf("\n Please enter marks for student %d\n", i);
scanf("%u", &list[i].marks);
}
}
void print_list(student list[], int SIZE)
{
int i;
printf("\t STUDENT NAME STUDENT MARKS\t \n");
for(i=0;i<SIZE;i++)
{
printf("\t %s \t %u\n", list[i].name, list[i].marks);
}
}
The program does give a correct output, but the warning remains.
Try this code:
for (i=0;i<SIZE;i++)
{
printf("\n Please enter name for student %d\n", i);
scanf("%s",list[i].name);
printf("\n Please enter marks for student %d\n", i);
scanf("%u", &list[i].marks);
}
This is because & used in scanf statement is to get the address.
In your case you use array name i.e. name and array name itself is providing the address. Remember array name gives the base address of an array.
change scanf("%s",&list[i].name); to scanf("%s",list[i].name);. Delete &. Because basically array name represents base address. No need to mention address of array for scanning the string.
Remove the & in the scanf that scan in a string using %s to eliminate the warning. So change
scanf("%s",&list[i].name);
to
scanf("%s",list[i].name);
This is because the name of the char array decays to a pointer to its first element
line 33:
scanf("%s",&list[i].name);//wrong
scanf("%s",list[i].name);//right
The name of an array is synonym for the location of the initial element , thus in your code, variable 'name' is the address of name[0]. You don't need to use & on 'name' to get the address of the array.Just use 'name' itself.