简体   繁体   English

c 使用结构体的数据库程序

[英]c database program using structs

I'm make a program in c that display a database using structures, I'm kinda new to programming also I know c++ and i make the program also for c++ but I have a problem in both programs.我正在用 c 编写一个程序,使用结构显示一个数据库,我对编程也有点陌生,我也知道 c++,我也为 c++ 编写了程序,但我在这两个程序中都有问题。 I will post only the c code.我只会发布c代码。 So the program works fine until I saw that is not printed the values I entered, it prints random values(it's garbage memory I guess).所以程序运行良好,直到我看到没有打印我输入的值,它打印随机值(我猜这是垃圾内存)。 I don't know why this is working have a look:我不知道为什么这是有效的看看:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct database{
char dataname[32],date[32];
int  number_employers;
float  profit;
double salary;
}database;
void new_company(database x);
void show_company(database x);

int main()
{
database Tarom,Wizzair;
new_company(Tarom);
system("cls");
new_company(Wizzair);
system("cls");
show_company(Tarom);
show_company(Wizzair);
return 0;
}

void new_company(database x){
printf("Enter company name: ");
scanf("%s",&x.dataname);
printf("\nEnter company date: ");
scanf("%s",&x.date);
printf("\nEnter number of employers:");
scanf("%d",&x.number_employers);
printf("\nEnter the average salary of employers:");
scanf("%f",&x.salary);
printf("\nEnter the company profit(in proccent):");
scanf("%f",&x.profit);
}

void show_company(database y){
printf("Company name:%s\n",y.dataname);
printf("Company date:%s\n",y.date);
printf("Number of employers:%d\n",y.number_employers);
printf("Average salary of employers:%f\n",y.salary);
printf("Company profit:%f\n***************************\n",y.profit);
}

void new_company(database x){

You passed the object by value, so the function gets a local copy.您按值传递对象,因此该函数获取本地副本。 Any changes to the object made inside the function affect only the local copy, not the original.在函数内部对对象所做的任何更改仅影响本地副本,而不影响原始副本。 That is your problem.那是你的问题。

In C++, the correction to that problem is simpler.在 C++ 中,对这个问题的修正更简单。 You just change the signature of the function to您只需将函数的签名更改为

void new_company(database& x){

In C, the change is more difficult.在 C 中,更改更加困难。 You need & in front of the object name where the caller passes it to the function, and you need * in front of the name in the function signature, and you need to change all the .调用者将其传递给函数的对象名称前面需要& ,函数签名中名称前面需要* ,并且需要更改所有. used to access members of the object inside the function to ->用于访问函数内部对象的成员到->

Because you're passing the database object by value, so a copy of the (uninitialized) dictionary objects is being passed in, and it's this copy that's being updated by new company.因为您是按值传递数据库对象,所以传入的是(未初始化的)字典对象的副本,而新公司正在更新该副本。 Pass in the address of the dictionaries, eg传入字典的地址,例如

new_company(&Tarom);

and change new_company definition to take a pointer to a database, like:并更改 new_company 定义以获取指向数据库的指针,例如:

void new_company(database* x){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM