Hey,
I'm writing a program that takes user input (names, addresses, phone numbers) puts it into a struct and keeps those records. I'm just getting stuck with how to properly assign the input to the elements. Here's my code
Code:
struct rec {
char *name;
char *address;
short addressLength, nameLength;
int phoneNumber;
};
...
int newRec()
{
char *nm;
char *addy;
int nLength, aLength;
int phone;
printf("\nEnter the Name: ");
scanf("%s", &nm);
printf("\nEnter the Address: ");
scanf("%s", &addy);
printf("\nEnter the telephone number: ");
scanf("%d", &phone);
struct rec record;
record.name = (char) malloc(strlen(nm)+1);
if (record.name == NULL)
{
printf("Error");
return 1;
}
nLength = strlen(nm)+1;
record.address = (char*) malloc(strlen(addy)+1);
if (record.address == NULL)
{
printf("Error");
return 1;
}
aLength = strlen(addy)+1;
strcpy(record.name, nm);
strcpy(record.address, addy);
record.addressLength = aLength;
record.nameLength = nLength;
record.phoneNumber = phone;
I've also tried with scanf and I constantly get a segmentation fault, and corrupted stack after entering the address or the telephone number. I'm not well versed with structs, any help would be greatly appreciated!