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);
}
I would suggest that your function should work as follows:
char buffer[256];
for(i=0;i<n;i++)
{
printf(".......Enter Contact Details %d ........\n",i+1);
do {
printf("Enter the First Name");
} while (scanf("%255s",buffer)!=1);
contacts[i].firstName = malloc(strlen(buffer)+1);
strcpy(contacts[i].firstName, buffer);
//...etc
So you use a single buffer to get the input, then allocate memory exactly the size of the input, plus a terminating null character, then copy the buffer to that memory and reuse the buffer for the next input.
Notes: the size of a char is always 1, so sizeof(char)
is not needed. Also, the result of malloc
should not be cast.
You ask the input in a do...while
loop and check that scanf
was able to read input. In scanf, the format specifier limits the input to 255 characters so the buffer will not overflow.
(char *)malloc(sizeof(char));
allocates only one byte of memory. If you want to store a string, say 20 chars long, the code is: (char *)malloc(sizeof(char) * 20 + 1); //explicitly showed +1 for clarity, based on comment.
(char *)malloc(sizeof(char) * 20 + 1); //explicitly showed +1 for clarity, based on comment.
EDIT
Generally it is a good idea to set the maximum possible size of the array in malloc
. But if the size is too large, and/or you are concerned about memory, do the following:
contacts[i].firstName = (char *)malloc(sizeof(char) * MAX_POSSIBLE_SIZE);
printf("Enter the First Name");
scanf("%s",contacts[i].firstName);
char *temp = (char *)realloc(contacts[i].firstName, strlen(contacts[i].firstName) + 1);
if(temp!=NULL) contacts[i].firstName = temp;
Do this for every string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.