1

I have to initialize tCoworking coworking by implementing init_coworking function that is declared at the end.

/* Constants *********************************************************/
#define nWorkSpaces 50
#define unlimited 2000

/* Types *************************************************************/
typedef enum {tableFlex, tableFix, officeFix} rate;
typedef char string[55];

typedef struct {
    int reservationId;
    float monthPayment;
} tContractAnnex;

typedef struct {
    int id;
    string name;
    int discount;
} tPartner;

typedef struct {
    int id;
    float surface;
    rate rateType;
} tWorkspace;

typedef struct {
    int partnerId;
    int month;
    int year;
    tContractAnnex annex;
} tContract;

typedef struct {
    tWorkspace workSpace[nWorkSpaces];
    tContract contract[unlimited];
    tPartner partner[unlimited];
} tCoworking;

/* Function declaration */

void init_coworking(tCoworking *coworking);

As you can see the problem I have is that tCoworking is a nested struct with array of stucts as data types.. So far I'm doing this in order to initialize it but it must be a better way to do it.

void init_coworking(tCoworking *coworking){
coworking = malloc(sizeof(tCoworking));

 coworking->partner[0].id = 0;
 coworking->partner[0].discount = 0;
 strcpy(coworking->partner[0].name, "");
 
 coworking->workSpace[0].id = 0;
 coworking->workSpace[0].rateType = 0;
 coworking->workSpace[0].surface = 0;
 
 
 coworking->contract[0].partnerId = 0;
 coworking->contract[0].year = 0;
 coworking->contract[0].month = 0;
 coworking->contract[0].annex.monthPayment = 0;
 coworking->contract[0].annex.reservationId = 0;
}
0

2 Answers 2

1
void init_coworking(tCoworking *coworking) {
 coworking && memset( coworking, 0, sizeof( tCoworking ) );
}

memset initializes a block of memory - of specifiable length - to a single byte value. Your example indicates that you desire zero-initialization of the entire object, so memset serves this purpose well.
NULL-check your input argument.

I recommend you not malloc or calloc in your function because your function signature implies that the caller is the owner of the tCoworking. If you malloc within your init_coworking() function, then you'll have created a new heap-allocated instance of a tCoworking with no clear ownership. You can try keep track of newly-allocated objects in some type of container, but that's going far beyond the scope of your question -- keep it simple.

Sign up to request clarification or add additional context in comments.

Comments

1

The simplest way is to use calloc. Like malloc it allocates memory for you but it also sets all the memory to zero.

However - more important is that your current function doesn't make sense. The memory you allocate is simply lost. The caller of init_coworking will never get the allocated and initialized memory.

Either you should:

  1. Not do any malloc(or calloc)

or

  1. Return the malloced pointer.

Since the prototype suggest that you get a tCoworking pointer, the most likely thing is that it's already allocated (in some way), i.e. you want option 1.

So just do:

void init_coworking(tCoworking *coworking){   // NO malloc
    memset(coworking, 0, sizeof(tCoworking));
}

In case you only want the first array member set to zero (like your code indicates) you may get a little performance improvement by:

void init_coworking(tCoworking *coworking){   // NO malloc
    memset(&coworking->partner[0], 0, sizeof(coworking->partner[0]));
    ... similar for the other arrays ...
}

but I doubt that's worth the trouble...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.