我正在做一个项目,其中我需要打印搜索的 struct ,但当我打印时,它进行第一次打印,然后退出运行,并出现以下错误: error个 我搜索了错误,这是动态内存的正常问题,但这不应该是我的情况,因为我只打印Wht在那里,我没有增加任何东西,根据我的知识.
打印功能:
void printCompany(Companies companies, int i) {
printf("\nNif: %d", companies.companies[i].Nif);
printf("\nName: %s", companies.companies[i].name);
printf("\nCategory: %s", companies.companies[i].categories);
}
struct :
#define COMPANY_INITIAL_SIZE 20
#define SEARCHES_INITIAL_SIZE 20
#define ACTIVITIES_INITIAL_SIZE 20
#define TEXT_MAX_SIZE 200
#define NAME_MAX_SIZE 50
#define EMAIL_MAX_SIZE 50
#define TITLE_MAX_SIZE 50
#define STREET_MAX_SIZE 70
#define LOCATION_MAX_SIZE 70
#define NIF_MIN_SIZE 100000000
#define NIF_MAX_SIZE 999999999
enum category {
microCompany = 1, pmeCompany, macroCompany
};
enum state {
inactive, active
};
enum searchCamp {
nif, businessArea, nameLocation
};
typedef struct {
char title[TITLE_MAX_SIZE];
char text[TEXT_MAX_SIZE];
char userName[NAME_MAX_SIZE];
char userEmail[EMAIL_MAX_SIZE];
} Coment;
typedef struct {
int nota;
char userName[NAME_MAX_SIZE];
char userEmail[EMAIL_MAX_SIZE];
} Rating;
typedef struct {
int Nif;
char name[NAME_MAX_SIZE];
enum category categories;
int businessAreaCode;
enum state states;
char street[STREET_MAX_SIZE];
char location[LOCATION_MAX_SIZE];
int postalCode;
int nComents;
Coment coments;
int nRating;
Rating ratings;
} Company;
typedef struct {
int counter, size;
Company *companies;
} Companies;
void freeCompanies(Companies *);
void loadCompanyFile(Companies *, char*);
void saveCompanyFile(Companies *, char *);
void insertCompanies(Companies*);
void expandCompanySize(Companies *);
void searchNifCompanies(Companies);
//void printCompany(Company);
void printCompany(Companies,int);
function that calls the 打印功能:
/**
* @brief This function compares the element
* @param companies The struct Companies.
* @param nif The unique value of a company
* @return if sucesseful returns the index of the company found, if unsucesseful returns -1.
*/
int searchNifCompany(Companies companies, int nif) {
int i;
for (i = 0; i < companies.counter; i++) {
if (companies.companies[i].Nif == nif) {
return i;
}
}
return -1;
}
/**
* @brief This function
* @param *companies The pointer of the struct Companies.
* @return
*/
void searchNifCompanies(Companies companies) {
int company, nif;
nif = getInt(NIF_MIN_SIZE, NIF_MAX_SIZE, "\nNIF[9 digits]: ");
company = searchNifCompany(companies, nif);
if (company != -1) {
//printCompany(companies.companies[company]);
printCompany(companies, company);
} else {
puts("The company doesn't exist.");
}
}
it gives me the same result even i manualy tell to print the struct :
Companies companies;
int main() {
loadCompanyFile(&companies, COMPANIES_FILE_NAME);
mainMenu();
printf("\nNif: %d", companies.companies[1].Nif);
printf("\nName: %s", companies.companies[1].name);
printf("\nCategory: %s", companies.companies[1].categories);
saveCompanyFile(&companies, COMPANIES_FILE_NAME);
freeCompanies(&companies);
return (EXIT_SUCCESS);
}