开发者

Confusion with typedef and pointers in C [duplicate]

This question already has answers here: Closed 11 ye开发者_StackOverflowars ago.

Possible Duplicate:

Typedef pointers a good idea?

I am confused with the following:

typedef struct body *headerptr;

Now, when I create something with type headptr that points to a struct body, to create a new headerptr would be as follows (I'm not sure if I'm doing this correctly):

headerptr newHeadptr;

Am I correct to assume that this would be a pointer that points to a struct body?


Yes. headerptr is now equivalent to struct body*.


This would be a pointer that points to a struct body.

The way you've declared it, newHeadptr could point to a struct body. Remember, though, that you haven't allocated a struct body for it to point to. Initially, newHeadptr will just have some garbage value. To rectify that, you could to this:

headerptr newHeaderptr = malloc(sizeof(*newHeaderptr));

or:

struct body newBody;
headerptr newHeaderptr = &newBody;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜