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)
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742

4 Answers4

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.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
4

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()
}

Ori Pessach
  • 6,777
  • 6
  • 36
  • 51
ForEveR
  • 55,233
  • 2
  • 119
  • 133
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.

jwodder
  • 54,758
  • 12
  • 108
  • 124
  • 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. – Drew Noakes Jun 21 '13 at 21:31
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++

Community
  • 1
  • 1
catfood
  • 4,267
  • 5
  • 29
  • 55