c++pointerssyntaxcompiler-errorsdynamic-allocation

error: request for member (maybe you meant to use '->' ?) while using '->' already


Is there an easy explanation for what this error means?

error: request for member 'Attributes' in '* printerInfo', which is of pointer type 'PPRINTER_INFO_2 {aka _PRINTER_INFO_2A*}' (maybe you meant to use '->' ?)

PPRINTER_INFO_2* printerInfo = NULL;

    void ChangedPrinter()
    {
       ...
       DWORD attributesPrinterInfo;

       printerInfo = (PPRINTER_INFO_2*) malloc(bufferSize);

       attributesPrinterInfo = printerInfo->Attributes; // error

       free(printerInfo);
    }

What am I doing wrong???


Solution

  • PRINTER_INFO_2 structure is defined as:

    typedef struct _PRINTER_INFO_2 {
      // ...
    } PRINTER_INFO_2, *PPRINTER_INFO_2;
    

    so PPRINTER_INFO_2 is pointer to PRINTER_INFO_2. When you do

    printerInfo = (PPRINTER_INFO_2*) malloc(bufferSize);
    

    printerInfo actually becomes a pointer to pointer to PRINTER_INFO_2. I'm not sure whether this was an intention or just a mistake, but if it's intended to be PPRINTER_INFO_2* then proper usage is:

    (*printerInfo)->Attributes