The typedef line doesn't make sense to me. A new name (randctx) for struct randctx? If I delete the typedef line it should still compile, but it doesn't. Any idea why? What's the point of having such a typedef line?

So that you can use the name randctx instead of struct randctx, but obviously this does not apply in C++.

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

The typedef line doesn't make sense to me. A new name (randctx) for struct randctx? If I delete the typedef line it should still compile, but it doesn't. Any idea why? What's the point of having such a typedef line?

In C, the declaration of struct randctx declares a struct tag named "randctx" and (effectively) a type named "struct randctx". The typedef makes randctx an alternative name for the type "struct randctx".

In C++, the declaration of struct randctx implicitly also declares a type named randctx, so the typedef is not required.

Hence you can expect the above to compile happily with a C compiler, but not with a C++ compiler (with some ambiguity in that statement depending on compatibility settings supported by some compilers).