4

I often see C++ code like this:

void foo()
{
  struct sockaddr* from;
  // ...
}

Why is the struct specifier needed? Does it actually do anything? The compiler can already know that sockaddr is declared as a struct, so I wonder why this is useful or necessary.

I've tried removing it in the past and haven't noticed a difference in behaviour, but I'm not confident it's safe to remove.

Similarly, what's the difference between these two?

sizeof(struct sockaddr)
sizeof(sockaddr)
6
  • Does not it come from old C years? Commented Jun 21, 2013 at 21:02
  • @Naszta "Does it not come from old C years?" - Sorry Commented Jun 21, 2013 at 21:02
  • @Naszta, I have wondered as much. If so, why would it be necessary in C but not in C++? Commented Jun 21, 2013 at 21:02
  • @DrewNoakes: FYI Commented Jun 21, 2013 at 21:04
  • You might want to read this answer to a related question. Commented Jun 21, 2013 at 21:10

4 Answers 4

5

As a coding "style", this is most likely a heritage of C, where the keyword is necessary.

In C++ this is not needed in most situations, although it is used sometimes to force the compiler to resolve a name to the name of a type rather than the name of some other entity (like a variable, as in the following example):

struct x
{
};

int main()
{
    int x = 42;
    struct x* pX = nullptr; // Would give a compiler error without struct
}

Personally, I do not consider good style having a variable, or a function with the same name as a type, and I prefer avoiding having to use the struct keyword for this purpose at all, but the language allows it.

Sign up to request clarification or add additional context in comments.

2 Comments

In your code example, could you also clarify the type using the typename keyword?
@DrewNoakes: No, typename cannot be used in this context. Basically typename can only be used inside templates.
5

This syntax comes from C, where it's obligatory. In C++ there is only one needed usage of this syntax

n3376 9.1/2

A class declaration introduces the class name into the scope where it is declared and hides any class, variable, function, or other declaration of that name in an enclosing scope (3.3). If a class name is declared in a scope where a variable, function, or enumerator of the same name is also declared, then when both declarations are in scope, the class can be referred to only using an elaborated-type-specifier

struct stat {
// ...
};
stat gstat; // use plain stat to
// define variable
int stat(struct stat*); // redeclare stat as function
void f() {
struct stat* ps; // struct prefix needed
// to name struct stat
stat(ps); // call stat()
}

Comments

1

Code such as this comes directly from C (most likely via copy & paste), where the struct keyword is required before the name of any type that is defined as a struct (unless you add the appropriate typedef statement). Using struct in this way is still supported in C++ for backwards compatibility, but because struct foo { ... }; in C++ automatically adds foo as a type name, it is (usually) safe to refer to the type afterwards as just foo.

1 Comment

Yes I usually see this in code that's using C APIs, such as the socket example in my original question. Makes sense, and I'll now feel more confident to strip it out most of the time.
1

You are largely correct; in C++ the struct keyword is hardly ever relevant in declaring a variable.

More than you ever wanted to know about this: using struct keyword in variable declaration in C++

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.