Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 9-Sep-18 7:45am

Couple of things here:
1) A mobile number is not a number in the traditional sense: it is a string, not an integer. Don't believe me? Phone your best mate without using a leading zero ...
If you can't do maths with it, it isn't a number, and should not be stored as such.
(And also, a mobile number can contain other characters such as brackets, and plus.)
This ignores that the size of integer varies from system to system: on some it may only be 16 bits, which is too short to holed a mobile number anyway.

2) When you do this:
C++
ptr->mobilenumber
you dereference the pointer and return the value that is contained in the integer, not the address of the integer. So when you do this:
scanf("%u", ptr->mobilenumber);
you pass the content of the variable to scanf - which is either random of zero depending on your compiler and options - and it will treat it as an address. At that point your app is very likely to crash or do very strange things!
You need to pass the address of the integer, not it's content:
scanf("%u", &(ptr->mobilenumber));
It works for arrays of characters because the name of an array is a pointer to the first element of the array itself.

What I would do is read the user input into an array of characters, and then process it to the variable you want, checking that it fits before I did it.
 
Share this answer
 
Comments
Zeeking99 9-Sep-18 6:50am    
Thank You so much. Now I understand why it is so important to understand pointers. Thank you for pointing a weakness in me.
OriginalGriff 9-Sep-18 6:54am    
You're welcome!
You should use and store a mobile phone number as string. Take a look at this examples of phone numbers.

When comparing phone numbers you need some normalization like removing the "()" or dealing with "+". And comparision is always right sided.

Dealing with phone numbers is an art. ;-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900