0

I'm not even a beginner of c language.

and I see the codes from cpython source code:

typedef struct _formatdef {
    char format;
    Py_ssize_t size;
    Py_ssize_t alignment;
    PyObject* (*unpack)(_structmodulestate *, const char *,
                        const struct _formatdef *);
    int (*pack)(_structmodulestate *, char *, PyObject *,
                const struct _formatdef *);
} formatdef;

in the 5th line, I know that PyObject* is defining a pointer to a variable of PyObject type, however what does (*unpack) mean? and what does the following (_structmodulestate *) mean?

Woods Chen
  • 574
  • 3
  • 13
  • 4
    Find your text-book, and read about *function pointers* (pointers to functions). – Some programmer dude Jan 21 '22 at 03:24
  • 1
    Please consider that if something seems well above your level, and understanding it also *isn't going to help you solve a problem*, that it might not be a productive use of your time at the moment. If you want a high-level overview of how languages like Python are implemented in languages like C, with conceptual discussion about the data structures etc., you can probably find those out there. Or at least try asking on an actual *discussion forum* such as Reddit or Quora. – Karl Knechtel Jan 21 '22 at 03:54
  • Also, a useful utility: https://cdecl.org – Karl Knechtel Jan 21 '22 at 03:55
  • I need the supporting for some odd data structures like 6 byte unsigned integers, and thought to change the source code of some python packages written in c (not too much work). that's why I came to this question. And I will read more as you suggested if that doesn't help. Than you @KarlKnechtel. – Woods Chen Jan 22 '22 at 07:35

1 Answers1

1

It's a function pointer declaration.

This:

float x;

declares a variable of type float named x.

This:

float *p;

declares a variable of type float* (a pointer to a float) named p.

This:

int foo(float *p);

declares a function named foo, which takes a float* as an argument, and returns an int.

This:

PyObject* (*unpack)(_structmodulestate *, const char *, const struct _formatdef *);

declares a function pointer named unpack, which points to a function which takes three pointers (of various types) as arguments, and returns a PyObject*.

Beta
  • 96,650
  • 16
  • 149
  • 150