简体   繁体   中英

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 :)

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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM