minc33
minc33

Reputation: 13

How do I pass a struct pointer to a function with constant value

I have:

typedef struct LIST *List;

and

struct LIST
{
 Node *head;
 int numNodes;
};

If I would like to pass an instance of List to a function and keep the the value pointed to constant, what would an appropriate prototype look like?

So far I've tried functions whose prototypes look like these:

void func1(const List list);

and

void func2(List const list);

Both seem to keep the the pointer itself constant where neither seem to keep the value pointed to constant.

Any help is much appreciated :)

Upvotes: 0

Views: 36

Answers (1)

Both seem to keep the the pointer itself constant where neither seem to keep the value pointed to constant.

Which is precisely why one should not hide pointer semantics if they intent to continue and treat the type as a pointer. Get rid of the obfuscating typedef:

void func1(struct LIST const *p_list);

Any attempt to reorder the const relative to the type is a red herring. A common case of "leading const is misleading".

Upvotes: 2

Related Questions