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 8819749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:26:27+00:00 2026-06-14T05:26:27+00:00

I have a struct like the following: typedef struct { player *lastmover; player *previous;

  • 0

I have a struct like the following:

typedef struct {
    player *lastmover;
    player *previous;
} lastmove;


typedef struct {
     int moves;
     char *name;
} player;

I try do a memory alloc like this so the :

lastmove lmv;
lmv.lastmover=malloc(sizeof(player *));
lmv.previous=malloc(sizeof(player *));
.....
callfunction(&lmv);
.....

then in another place i use a pointer lmvp (lastmover *) and do assignment like this:

void
callfuntion(lastmove *)
{
      .....
      lmvp->previous=lmvp->lastmover;
      lmvp->lastmover=p;     //where p is of type (player *)
      .....
}

this all works fine, but I don’t know how to control weather previous mover is initialized. In the first game move in the program the lastmover variable (player *) is initialized, but the previous variable that is assigned to NULL(or garbage?) lmvp->previous=lmvp->lastmover;. But I want somehow to check if the previous mover is initialized or not. Im trying this:

void
callfunction(lastmove *)
{
     ......
     ......
     if(lmvp->previous!=NULL)
     ......
}

But im quite certain it will not do.. How is the best way to control this?

  • 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-06-14T05:26:28+00:00Added an answer on June 14, 2026 at 5:26 am

    you’re only allocating size of pointer, this

    lmv.lastmover=malloc(sizeof(player *));
    

    Should be

    lmv.lastmover=malloc(sizeof(player));
    

    this all works fine

    it shouldn’t 🙂

    Edit:
    if you only assign a pointer to lastmover then you don’t need to allocate memory at all, otherwise it’s a memory leak, now if you want to check if it’s initialized then initialize it to NULL

    lastmove lmv = {0};
    //or
    lmv.lastmover= NULL;
    lmv.previous= NULL;
    ...
    //later
    if(lmv->previous!=NULL)
    

    A third way to do it, in C99, is using designated initializers:

    lastmove lmv = {
       .lastmover= NULL;
       .previous= NULL;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a struct like the following ... typedef struct { int WheelCount;
I have a struct like this typedef struct bookStruct { char title[80]; char author[80];
If I have a struct like this: typedef struct { unsigned char c1; unsigned
Question 1 I have a struct like, struct foo { int a; char c;
I have a structure defined like so: typedef struct { int n; int *n_p;
I have the following struct: typedef struct { int someArray[3][2]; int someVar; } myStruct;
I have code that looks like the following: #include <stdio.h> typedef struct SomeStruct* ptr;
I have struct which is defined in c++ Win32 DLL like the following: typedef
I have the following struct in C++: #define MAXCHARS 15 typedef struct { char
I have a struct like this: typedef struct tgPoligono { int numero_de_lados; CvPoint** cantos;

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.