Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7168119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:42:55+00:00 2026-05-28T14:42:55+00:00

I have few questions connected with struct and typedef, there is piece of code

  • 0

I have few questions connected with struct and typedef, there is piece of code and I have marked some places where I’m not sure if the syntax is correct. I use Eclipse editor and it shows me when there is problem in compilation. I just don’t understand why is sometimes keyword struct needed and sometimes not. I may have also some mistakes of using this keyword. So plese help me to understand it.

let’s have struct

 typedef struct player
    {
        char *name; 
        int  gameId; 
        int  points; 
        int socketfd; //socket descriptor of player
        int state;
    } player_struct;

lets have another struct

#define PLAYERSLEN 2
typedef struct game{
    struct player_struct *players[PLAYERSLEN]; //PLACE1
    //some code

} game_struct;

let’s have function

player_struct *create_player()  //PLACE2
    {
    player_struct *player;  //PLACE3

    //alokace pameti
    player = (player_struct *) malloc(sizeof(player_struct)); //PLACE4
    //PLACE5

    player->gameId = -1;
    player->points = 0;
    player->state = 0;

    return player;
    }

let’s have function? In fact what does this definition mean?

void *( player_struct *player) //PLACE6
        {

        //some code

        }

Questions references:
PLACE1 – is this correct? why can’t I use just player_struct *players[PLAYERSLEN]; ??

PLACE2 – it looks like there is not needed struct before player_struct , is it correct? why?
PLACE3 – it looks like there is struct also not needed, is it correct? why?
PLACE4 – it looks like there is struct also not needed, is it correct? why?
PLACE4 & PLACE5 I may should handle errors there, cause there is malloc so I should probably put all the line with PLACE4 to if and if the malloc fails I should put at PLACE 5 free(player). Am I right?
PLACE6 what could have mean this function or whatever it is? The code inside brackets which is not included here should delete the player .. I just don’t understand the syntax of wrote function – what does it mean?
PLACE6 – again similar as previous why is not necessary put the keyword struct before player_struct at this line? is it correct?

Really thanks you for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:42:56+00:00Added an answer on May 28, 2026 at 2:42 pm
    1. No. player_struct is a typedef for struct player. You don’t need the struct keyword in this case.
    2. Correct. You created a type definition called player_struct which means struct player.
    3. Same reason as 2.
    4. Same reason as 2 & 3.
    5. Yes, you should check to see if malloc() returns NULL. In addition, don’t cast the return value from malloc(). Doing so can hide #include errors.
    6. That’s not a valid function definition. You don’t need struct because of the typedef – same as above.

    You may find this all makes more sense if you remove the words typedef, player_struct, and game_struct from your code entirely. Then once you get used to how that all works, you can reintroduce the typedefs and maybe cut down on some typing. As a quick example, you can break down your first definition into its components:

    struct player
    {
      char *name; 
      int  gameId; 
      int  points; 
      int socketfd; //socket descriptor of player
      int state;
    };
    
    typedef struct player player_struct;
    

    Maybe that will help you make sense of it?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few questions connected to Android In-App Billing: Is it possible to
I have few questions about using lock to protect my shared data structure. I
I have few questions, I want to show the alert window on my Update
I have a few questions about Xcode and interaction with GCC 4.2.1: It doesn't
I have a few questions about this after reading the iPhone documentation on it:
I have a few questions related to Windows processes in kernel and usermode. If
I have a few questions relating to setjmp/longjmp usage - What is the use
I have a few questions about cross-page posting in ASP.NET: What is cross-page posting
I have a few questions: Why was a.out replaced by ELF ? What were
I have a few questions about Core Location. 1) Should the user refuse permission

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.