1

In his book Patterns in C, Adam Petersen describes using a pointer to a struct, which is declared in a header file, to create a first-class abstract data-type:

/* Customer.h */
/* A pointer to an incomplete type (hides the implementation details). */
typedef struct Customer* CustomerPtr;
/* Create a Customer and return a handle to it. */
CustomerPtr createCustomer(const char* name, const Address* address);
/* Destroy the given Customer. All handles to it will be invalidated. */
void destroyCustomer(CustomerPtr customer);

The struct and the two functions are defined in Customer.c:

#include ”Customer.h”
#include <stdlib.h>
struct Customer
{
    const char* name;
    Address address;
    size_t noOfOrders;
    Order orders[42];
};
CustomerPtr createCustomer(const char* name, const Address* address)
{
    CustomerPtr customer = malloc(sizeof * customer);
    if(customer)
    {
        /* Initialize each field in the customer... */
    }
    return customer;
}
void destroyCustomer(CustomerPtr customer)
{
    /* Perform clean-up of the customer internals, if necessary. */
    free(customer);
}

The way I would organize my code is a bit different. The header file would declare the struct itself, not a pointer to it. Here is the way I was taught of coding something similar:

/* Customer.h */
struct Customer;
extern struct Customer* newCustomer(const char* name);

and declarations:

/* Customer.c */
struct Customer {
    char name[CUSTNAMELENGTH +1];
};
struct Customer* newCustomer(const char* name) {
    struct Customer* newCustomer;
    newCustomer = malloc(sizeof(struct Customer));
    /* initialize customer instance */
    return newCustomer;
}

What is the practical difference between the two styles? Is there a potential problem with my way that I don't see? It seems to successfully encapsulate the object, and provide an interface similar to Petersen's design. When should I use his design rather than what I have been doing?

3

These are not that different.

Both .h files effectively declare struct Customer as a forward declaration. The first one also defines a pointer type, which the second doesn't. Some prefer the typedef as they don't like to write struct and *; I would probably agree.

The extern declaration is good, but not necessary, I believe. Otherwise, the naming createCustomer vs. newCustomer is fairly arbitrary to me.

One could be more critical about the two styles except that the neither is very complete; the second example even less so.


FYI, Both of these have potential errors.

The first malloc's only a single pointer instead of a struct (it references struct * customer instead of struct Customer, so it won't compile either). It also allocates a fixed size of 42 for the orders, which doesn't make a lot of sense to me; it would probably be better to make it an expandable data structure (although, the astute reader may note that in C/C++ a structure that ends with an array is potentially expandable -- still if one were doing that they'd use something like zero as the array size.)

The second assumes a fixed customer name length, which if sufficiently large is wasteful when not needed, and, presumably can still be exceeded. It would probably be better to allocate strings to size, which would avoid both the space waste and the overflow issue.

  • 2
    Are you saying that a typedef pointer to struct is effectively the same as declaring the struct itself? If so, that's great! I prefer the latter, because it's more explicit, and I'm not smart enough to keep track of all those typedefs. – ridthyself Oct 4 '16 at 14:49
  • 2
    (1) The language takes ... struct X ... without the {}'s, in certain contexts, as a forward declaration. It doesn't have to be a pointer being typedef'd, you could say typedef struct X Y; which would declare X as a forward struct and Y as equivalent to X. (2) That's why the ...Ptr is added to the typedef. FYI, you can declare two pointers: CustomerPtr x,y; or struct Customer *x,*y; but beware struct Customer *x,y; FYI, CustomerPtr is arguably more abstract that struct Customer *. – Erik Eidt Oct 4 '16 at 14:55
2

I prefer your approach: presenting the API context explicitly as a pointer to a structure.

Some people think presenting the context instead as an unspecified "thing" is cleaner, so they use a typedef to hide the structure and the pointer. I cannot say they're wrong, but my opinion is that, to a C programmer, a pointer to a structure just as abstract as a typedef of unknown type.

There is one small advantage, though, and that's that the header file is in control of what the context actually is. So, for example, an API might start its life with a context that's just an integer ("typedef int CustomerContext") could later change it to a pointer to a structure ("typedef struct Customer *CustomerContext") without requiring any changes to the code using the API (as long as the user code doesn't make invalid assumptions about whether the context is a pointer or an integer). But that doesn't really matter if your API is starting life with a structure already holding the context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.