本文介绍了获取工人的详细信息并将其写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在学习C.我正在尝试编写一个程序,该程序询问工作人员数量并使用malloc函数分配内存。我创建了一个存储工人详细信息的结构。当我从用户那里获取详细信息时,一切正常,直到程序询问工人手机号码,程序停止工作。



#包括< stdio.h> 
#include< stdlib.h>

typedef struct
{
int eid;
char name [50];
unsigned int mobilenumber;
}工人;

int main()
{
int worker;

printf(输入工人数量:);
scanf(%d,& worker);

工人* ptr;
ptr =(工人*)malloc(工人* sizeof(工人));

printf(输入工人的移动数字:);
scanf(%u,ptr-> mobilenumber);
printf(输入工人的ID:);
scanf(%d,ptr-> eid);
printf(输入工人姓名:);
scanf(%[^ \ n],ptr-> name);

printf(%d,ptr-> eid);

free(ptr);
返回0;
}





我的尝试:



我认为问题是因为s​​canf函数而存在,但我无法找到错误。

解决方案



I am still learning C. I am trying to write a program which asks for number of workers and assigns memory using malloc function. I created a structure which stores the details of the workers. When I am taking the details from the user everything works fine until the program asks for the workers mobile number the program stops to work.

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
	int eid;
	char name[50];
	unsigned int mobilenumber;
} workers;

int main()
{
	int worker;

	printf("Enter the number of workers: ");
	scanf("%d", &worker);

	workers *ptr;
	ptr = (workers *)malloc( worker * sizeof(workers));

	printf("Enter the mobilenumber of the worker: ");
	scanf("%u", ptr->mobilenumber);
	printf("Enter the ID of the worker: ");
	scanf("%d", ptr->eid);
	printf("Enter the name of the worker: ");
	scanf(" %[^\n]", ptr->name);

	printf("%d", ptr->eid);

	free(ptr);
	return 0;
}



What I have tried:

I think the problem exists because of the scanf function but I am not able to find the fault.

解决方案



这篇关于获取工人的详细信息并将其写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:31