Skip to main content
added 2 characters in body
Source Link
LPs
  • 16.2k
  • 8
  • 34
  • 64

As you wrote, to allocate space for all_accounts_dyn you used

*all_accounts = malloc(sizeof(struct Account)*num_accounts);

It is correct due to the double pointer. 

The double pointer takes the address of pointer all_accounts_dyn

The returned address of malloc is assigned to all_accounts_dynall_accounts_dyn that is *all_accounts inside the dynamicStruct function.

So when you loop to init the struct you have to first dereference the double pointer and the dereference all_accounts_dyn items.

    for(int i = 0; i<num_accounts;i++)
    {
        (*all_accounts)[i].accountNo = i;

        printf("initialized %d\n", (*all_accounts)[i].accountNo);

    }

As you wrote, to allocate space for all_accounts_dyn you used

*all_accounts = malloc(sizeof(struct Account)*num_accounts);

It is correct due to the double pointer. The double pointer takes the address of pointer all_accounts_dyn

The returned address of malloc is assigned to all_accounts_dyn that is *all_accounts inside the dynamicStruct function.

So when you loop to init the struct you have to first dereference the double pointer and the dereference all_accounts_dyn items.

    for(int i = 0; i<num_accounts;i++)
    {
        (*all_accounts)[i].accountNo = i;

        printf("initialized %d\n", (*all_accounts)[i].accountNo);

    }

As you wrote, to allocate space for all_accounts_dyn you used

*all_accounts = malloc(sizeof(struct Account)*num_accounts);

It is correct due to the double pointer. 

The double pointer takes the address of pointer all_accounts_dyn

The returned address of malloc is assigned to all_accounts_dyn that is *all_accounts inside the dynamicStruct function.

So when you loop to init the struct you have to first dereference the double pointer and the dereference all_accounts_dyn items.

    for(int i = 0; i<num_accounts;i++)
    {
        (*all_accounts)[i].accountNo = i;

        printf("initialized %d\n", (*all_accounts)[i].accountNo);

    }
added 2 characters in body
Source Link
LPs
  • 16.2k
  • 8
  • 34
  • 64

As you wrote, to allocate space for all_accounts_dyn you used

*all_accounts = malloc(sizeof(struct Account)*num_accounts);

It is correct due to the double pointer. The double pointer takes the address of pointer all_accounts_dyn

theThe returned address of malloc is assigned to all_accounts_dyn that is *all_accounts inside the dynamicStruct function.

So when you loop to init the struct you have to first dereference the double pointer and the dereference all_accounts_dyn items.

    for(int i = 0; i<num_accounts;i++)
    {
        all_accounts[i]->accountNo(*all_accounts)[i].accountNo = i;

        printf("initialized %d\n", (*all_accounts)[i].accountNo);

    }

As you wrote, to allocate space for all_accounts_dyn you used

*all_accounts = malloc(sizeof(struct Account)*num_accounts);

It is correct due to the double pointer. The double pointer takes the address of pointer all_accounts_dyn

the returned address of malloc is assigned to all_accounts_dyn that is *all_accounts inside the dynamicStruct function.

So when you loop to init the struct you have to first dereference the double pointer and the dereference all_accounts_dyn items.

    for(int i = 0; i<num_accounts;i++)
    {
        all_accounts[i]->accountNo = i;

        printf("initialized %d\n", (*all_accounts)[i].accountNo);

    }

As you wrote, to allocate space for all_accounts_dyn you used

*all_accounts = malloc(sizeof(struct Account)*num_accounts);

It is correct due to the double pointer. The double pointer takes the address of pointer all_accounts_dyn

The returned address of malloc is assigned to all_accounts_dyn that is *all_accounts inside the dynamicStruct function.

So when you loop to init the struct you have to first dereference the double pointer and the dereference all_accounts_dyn items.

    for(int i = 0; i<num_accounts;i++)
    {
        (*all_accounts)[i].accountNo = i;

        printf("initialized %d\n", (*all_accounts)[i].accountNo);

    }
Source Link
LPs
  • 16.2k
  • 8
  • 34
  • 64

As you wrote, to allocate space for all_accounts_dyn you used

*all_accounts = malloc(sizeof(struct Account)*num_accounts);

It is correct due to the double pointer. The double pointer takes the address of pointer all_accounts_dyn

the returned address of malloc is assigned to all_accounts_dyn that is *all_accounts inside the dynamicStruct function.

So when you loop to init the struct you have to first dereference the double pointer and the dereference all_accounts_dyn items.

    for(int i = 0; i<num_accounts;i++)
    {
        all_accounts[i]->accountNo = i;

        printf("initialized %d\n", (*all_accounts)[i].accountNo);

    }