-2

I'm setting up a basic socket server following this guide: https://www.geeksforgeeks.org/socket-programming-cc/

So, I know that when you use the keyword struct it's to define a data structure, but as you can see in the example, it creates an instance of sockaddr_in using the structure keyword, but it's not for creating/defining the structure, it's to create an instance.

So I'm wondering why in this guide to define a sockaddr_in structure he puts the 'structure' keyword before, I tried it without and it compiles all correctly.

Is there any reason behind?

2
  • in c you need to put the keyword when declaring an instance of a struct. Iirc it is something about structs living in a different namespace. Almost sure there is a duplicate somewhere... Commented Aug 16, 2018 at 7:25
  • 1
    well, not the best dupe, but the answer explains the reasons Commented Aug 16, 2018 at 7:28

3 Answers 3

7

using struct when declaring an instance of a struct comes from C. It is optional in C++.

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

Comments

2

The source files shown in your link are both written in C, not C++. In C, when declaring an instance of a struct, you need the struct keyword, like this.

struct sockaddr_in address; 

In C++ you don't need to do this anymore.

1 Comment

"needed" ? I dont know C, but I think it you still need it
1

The article that you've mentioned is written in C, which in C when you create a struct and you try to use it as a type, you have to mention that it's a struct. If you want to give up on the struct, you can read more about the typedef operator.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.