I have an application to work, where i need to use an array of structure type as a member of structure . I have created models with structure but as you can see I need a member as an array inside the contact structure(dynamically depending on the user's input). I am allocating the size for it in the getContacts function by passing a variable named contactPersonLength from main,but as soon as the getContacts returns to main value of contactPersonLength variable changes and garbage value gets printed in main. So i am having trouble passing the varible to print function. Please tell me where i am wrong and how to allocate size for array which is a structure member
struct address
{
char *name,*street,*district;
int doorNo;
};
struct contactPerson
{
char *name,*contactNumber;
};
struct contact
{
char *firstName,*lastName,*emailId;
struct address billingAddress;
struct address shippingAddress;
struct contactPerson contactPersons[];
};
void getContacts(struct contact *contacts,int n,int contactPersonLength)
{
int isSame,i,j;
for(i=0;i<n;i++)
{
printf(".......Enter Contact Details %d ........\n",i+1);
contacts[i].firstName = (char *)malloc(sizeof(char));
printf("Enter the First Name");
scanf("%s",contacts[i].firstName);
contacts[i].lastName = (char *)malloc(sizeof(char));
printf("Enter the Last Name");
scanf("%s",contacts[i].lastName);
contacts[i].emailId = (char *)malloc(sizeof(char));
printf("Enter the email id");
scanf("%s",contacts[i].emailId);
printf(".....Billing address Details.....\n");
contacts[i].billingAddress.name = (char *)malloc(sizeof(char));
printf("Enter the name");
scanf("%s",contacts[i].billingAddress.name);
printf("Enter the DoorNo");
scanf("%d",&contacts[i].billingAddress.doorNo);
contacts[i].billingAddress.street = (char *)malloc(sizeof(char));
printf("Enter the Street name");
scanf("%s",contacts[i].billingAddress.street);
contacts[i].billingAddress.district = (char *)malloc(sizeof(char));
printf("Enter the District");
scanf("%s",contacts[i].billingAddress.district);
printf(".....Shipping Address Details....\n");
printf("Is your Shipping Address same as Billing Address Press 1 Or else Press 0");
scanf("%d",&isSame);
if(isSame==1)
{
contacts[i].shippingAddress = contacts[i].billingAddress;
}
else
{
contacts[i].shippingAddress.name = (char *)malloc(sizeof(char));
printf("Enter the name");
scanf("%s",contacts[i].shippingAddress.name);
printf("Enter the DoorNo");
scanf("%d",&contacts[i].shippingAddress.doorNo);
contacts[i].shippingAddress.street = (char *)malloc(sizeof(char));
printf("Enter the Street name");
scanf("%s",contacts[i].shippingAddress.street);
contacts[i].shippingAddress.district = (char *)malloc(sizeof(char));
printf("Enter the District");
scanf("%s",contacts[i].shippingAddress.district);
}
printf(" ContactPersonLength %d \n",contactPersonLength);
contacts[i].contactPersons[contactPersonLength];
for(j=0;j<contactPersonLength;j++)
{
printf(".....ContactPerson %d.....\n",j+1);
contacts[i].contactPersons[j].name = (char *)malloc(sizeof(char));
printf("Enter Contact Person Name");
scanf("%s",contacts[i].contactPersons[j].name);
contacts[i].contactPersons[j].contactNumber = (char *)malloc(sizeof(char));
printf("Enter Contact Person Contact Number");
scanf("%s",contacts[i].contactPersons[j].contactNumber);
}
}
}
void main()
{
struct contact contacts[n];
getContacts(contacts,n,contactPersonLen);
}