Thread: What exactly is the point of a struct definition like this:

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    6

    Question What exactly is the point of a struct definition like this:

    Hey guys,

    Was hoping to know what exactly the point of the following struct definition is:

    Code:
    typedef struct {
        Cursor cursor;
    } Cur;
    I'm going through the code within one of the source files for dwm (specifically drw.h). I don't really understand what exactly this achieves. I understand the concept of structs, but perhaps not so much so that I understand the significance of this declaration in favor of something like
    Code:
     typdef Cursor Cur
    in this instance.

    Is anyone able to tell me the reasoning behind this?

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Context is everything, and I don't have much to go on. Either:

    - sSomebody's bad attempt at saving typing three letters. Not likely

    - somebody was adding a level of abstraction, so in the future they could store other things along with cursor or even replace it with something different and not have to rewrite all the code that uses "Cur".

    - Maybe it is setting up for an Opaque data type. Reading Opaque data type - Wikipedia may give it context.

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    6
    Quote Originally Posted by hamster_nz View Post
    Context is everything, and I don't have much to go on.
    Understood. So grepping through the source file (dwm.c - dwm - dynamic window manager) it's used in the following way (I've prefixed lines with the actual line number within dwm.c which includes drw.h that defines the struct). Line 61
    Code:
     enum {CurNormal, CurResize, CurMove, CurLast}
    Line 1567
    Code:
    cursor[CurResize]   = drw_cur_create (drw, XC_sizing);
    Line 1307 (used during function invocation)
    Code:
    cursor[CurResize]->cursor
    There doesn't seem to be any other way which it is used throughout the program that deviates from lines like 1567 and 1307 shown. Thank you for the link, I'll have a look at that.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    /* Cursor abstraction */
    Cur *drw_cur_create(Drw *drw, int shape);
    void drw_cur_free(Drw *drw, Cur *cursor);
    Is there a reason you did NOT see the two lines above in the header file?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Jul 2018
    Posts
    6
    Quote Originally Posted by stahta01 View Post
    Code:
    /* Cursor abstraction */Cur *drw_cur_create(Drw *drw, int shape);void drw_cur_free(Drw *drw, Cur *cursor);
    Is there a reason you did NOT see the two lines above in the header file?Tim S.
    I completely overlooked that... So that answers my question then. It was used as a means of abstraction as mentioned by hamster_nz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-30-2011, 03:24 PM
  2. struct definition
    By kiros88 in forum C Programming
    Replies: 8
    Last Post: 08-25-2009, 06:07 PM
  3. #define within struct definition?
    By fanoliv in forum C Programming
    Replies: 11
    Last Post: 06-23-2006, 06:24 PM
  4. Replies: 1
    Last Post: 05-05-2004, 06:58 AM
  5. struct definition
    By chrismiceli in forum C Programming
    Replies: 2
    Last Post: 06-03-2003, 12:00 PM

Tags for this Thread