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
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);
}
}
#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;
}
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;
}
I am receiving the error in function main for my clrscr(); but I thought I had to clear when using fflush(stdin);?
I feel like I am missing something simple here but if anyone can shed some like I would appreciate it!
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct person
{
char name[10];
int age;
};
typedef struct person NAME;
NAME stud[10], temp[10];
void main()
{
int no,i;
void sort(int N); /* Function declaration */
clrscr();
fflush(stdin);
printf("Enter the number of students in the list\n");
scanf("%d",&no);
for(i = 0; i < no; i++)
{
printf("\nEnter the name of person %d : ", i+1);
fflush(stdin);
gets(stud[i].name);
printf("Enter the age of %d : ", i+1);
scanf("%d",&stud[i].age);
temp[i] = stud[i];
}
printf("\n*****************************\n");
printf (" Names before sorting \n");
/* Print the list of names before sorting */
for(i=0;i<no;i++)
{
printf("%-10s\t%3d\n",temp[i].name,temp[i].age);
}
sort(no); /* Function call */
printf("\n*****************************\n");
printf (" Names after sorting \n");
printf("\n*****************************\n");
/* Display the sorted names */
for(i=0;i<no;i++)
{
printf("%-10s\t%3d\n",stud[i].name,stud[i].age);
}
printf("\n*****************************\n");
} /* End of main() */
/* Function to sort the given names */
void sort(int N)
{
int i,j;
NAME temp;
for(i = 0; i < N-1;i++)
{
for(j = i+1; j < N; j++)
{
if(strcmp(stud[i].name,stud[j].name) > 0 )
{
temp = stud[i];
stud[i] = stud[j];
stud[j] = temp;
}
}
}
} /* end of sort() */
Put the function prototype void sort(int N); outside main()
You don't have (but you may) execute clrscr() before fflush(stdin). In this case contents of your screen (which you want to clear) have nothing to do with stdin.
You can read more about fflush() and the motivation to use it, here.
I assume that you get a compilation error. It is caused by the line above the one where you see the error.
As suggested by #Catalyst, it is caused here by the line
void sort(int N); /* Function declaration */`
because C does not allow functions to be declared locally inside other functions (and main is a function).
You can simply fix it that way :
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct person
{
char name[10];
int age;
};
typedef struct person NAME;
NAME stud[10], temp[10];
void sort(int N); /* Function declaration */
int main() // void main is incorrect
{
int no,i;
clrscr();
fflush(stdin);
...
Note also the int main() instead of void main(). It is harmless on Windows, but is still incorrect.
I have come across this wierd and mysterous (at least to me) error that I am finding a very hard time finding. It gives me an error at the line where I call my function input(student_list1[MAX], &total_entries); where the compiler says:
incompatible type for agument 1 in 'input'
What am I doing wrong here? I sense it something very simple and stupid but I have gone through the code several times now without any avail.
#define MAX 10
#define NAME_LEN 15
struct person {
char name[NAME_LEN+1];
int age;
};
void input(struct person student_list1[MAX], int *total_entries);
int main(void)
{
struct person student_list1[MAX];
int total_entries=0, i;
input(student_list1[MAX], &total_entries);
for(i=0; i<total_entries; i++)
{
printf("Student 1:\tNamn: %s.\tAge: %s.\n", student_list1[i].name, student_list1[i].age);
}
return 0;
} //main end
void input(struct person student_list1[MAX], int *total_entries)
{
int done=0;
while(done!=1)
{
int i=0;
printf("Name of student: ");
fgets(student_list1[i].name, strlen(student_list1[i].name), stdin);
student_list1[i].name[strlen(student_list1[i].name)-1]=0;
if(student_list1[i].name==0) {
done=1;
}
else {
printf("Age of student: ");
scanf("%d", student_list1[i].age);
*total_entries++;
i++;
}
}
}
struct person student_list1[MAX] in the function argument is actually a pointer to struct person student_list1.
student_list1[MAX] you passed is a (out of bound) member of the array struct person student_list1[MAX]. Valid array index shoudl be between 0 to MAX - 1.
Change it to:
input(student_list1, &total_entries);
Note that here the array name student_list1 is automatically converted to a pointer to student_list1[0].
There are many things wrong with the code; this is my attempt at making it somewhat more robust:
#include <stdio.h>
#include <string.h>
#define MAX 10
#define NAME_LEN 15
// use a typedef to simplify code
typedef struct person {
char name[NAME_LEN];
int age;
} person_t;
// size qualifier on student_list is redundent and person_t* does the same
void input(person_t *student_list, int *total_entries);
int main(void)
{
person_t student_list[MAX];
int total_entries, i;
// pass array and not the non-existent 'student_list[MAX]' element
input(student_list, &total_entries);
for(i=0; i<total_entries; i++)
{
// age is an int, not a string so use %d
printf("Student 1:\tName: %s.\tAge: %d.\n", student_list[i].name, student_list[i].age);
}
return 0;
} //main end
void input(person_t *student_list, int *total_entries)
{
int done = 0, i = 0;
*total_entries = 0;
while (i < MAX) {
printf("Name of student: ");
// use NAME_LEN instead of strlen(list[i].name) because latter is
// probably not initialized at this stage
if (fgets(student_list[i].name, NAME_LEN, stdin) == NULL) {
return;
}
// detect zero-length string
if (student_list[i].name[0] == '\n') {
return;
}
printf("Age of student: ");
scanf("%d", &student_list[i].age);
// read the newline
fgetc(stdin);
*total_entries = ++i;
}
}
input(student_list1[MAX], &total_entries); shoud be input(student_list1, &total_entries);.
In C,
void input(struct person student_list1[MAX], int *total_entries);
equals
void input(struct person *student_list1, int *total_entries);