簡體   English   中英

在結構內為結構數組分配內存

[英]Allocating memory for Structure Array within a Structure

我有一個要工作的應用程序,在這里我需要使用結構類型的數組作為structure的成員。 我已經創建了具有結構的模型,但是正如您所看到的,我需要一個作為接觸結構內部數組的成員(動態地取決於用戶的輸入)。 我正在通過從main傳遞一個名為contactPersonLength的變量來在getContacts函數中為其分配大小,但是一旦getContacts返回到contactPersonLength的主值,變量就會更改並且垃圾值會在main中打印。 所以我在傳遞變量到打印功能時遇到了麻煩。 請告訴我我錯了,以及如何為結構成員的數組分配大小

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);
}

我建議您的功能應如下所示:

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

因此,您可以使用單個緩沖區來獲取輸入,然后為內存分配與輸入大小完全相同的內存,再加上一個終止的空字符,然后將緩沖區復制到該內存,然后將該緩沖區重新用於下一個輸入。

注意:char的大小始終為1,因此不需要sizeof(char) 另外,不應malloc的結果。

您在do...while循環中詢問輸入,並檢查scanf是否能夠讀取輸入。 在scanf中,格式說明符將輸入限制為255個字符,因此緩沖區不會溢出。

(char *)malloc(sizeof(char)); 僅分配一個字節的內存。 如果要存儲一個字符串,比如說20個字符長,則代碼為: (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.


編輯

通常,最好在malloc中設置數組的最大可能大小。 但是,如果大小太大,和/或您擔心內存,請執行以下操作:

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;

對每個字符串都執行此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM