Pass a struct as parameter

0

I need this struct

typedef struct _wfs_ptr_retract_bins
{
    WORD                 wRetractBin;
    USHORT               usRetractCount;
} WFSPTRRETRACTBINS, *LPWFSPTRRETRACTBINS;

Turn a parameter that I'll put here.

typedef struct _wfs_ptr_status
{
...
LPWFSPTRRETRACTBINS *lppRetractBins;
} WFSPTRSTATUS, *LPWFSPTRSTATUS;

How do I do it?

I'm trying to do this:

WFSPTRSTATUS PtrStatus;
LPWFSPTRRETRACTBINS RetractBins;

RetractBins->wRetractBin = WFS_PTR_RETRACTBININSERTED;
RetractBins->usRetractCount = 0;

PtrStatus.lppRetractBins = &RetractBins;//AQUI QUERO PASSAR
    
asked by anonymous 04.02.2017 / 01:05

1 answer

1

No object was created and you are modifying struct members by a pointer that does not point to anything. This is undefined behavior.

The error is in:

LPWFSPTRRETRACTBINS RetractBins;

RetractBins->wRetractBin = WFS_PTR_RETRACTBININSERTED;
RetractBins->usRetractCount = 0;

Here RetractBins is a _wfs_ptr_retract_bins pointer that points to no objects.

The right thing is to first create a WFSPTRRETRACTBINS object and then pass its memory address to the RetractBins pointer.

The correct code would be: (without using windows.h )

#include <iostream>

#define WFS_PTR_RETRACTBININSERTED 5

typedef unsigned short USHORT;
typedef unsigned short WORD;

typedef struct _wfs_ptr_retract_bins
{
    WORD                 wRetractBin;
    USHORT               usRetractCount;
} WFSPTRRETRACTBINS, *LPWFSPTRRETRACTBINS;

typedef struct _wfs_ptr_status
{
LPWFSPTRRETRACTBINS *lppRetractBins;
} WFSPTRSTATUS, *LPWFSPTRSTATUS;



int main()
{
    WFSPTRSTATUS PtrStatus;
    WFSPTRRETRACTBINS objRetractBins; //primeiro criar um objeto válido na memoria
    LPWFSPTRRETRACTBINS RetractBins;

    RetractBins = &objRetractBins; // agora sim, o ponteiro RetractBins aponta para um objeto

    RetractBins->wRetractBin = WFS_PTR_RETRACTBININSERTED;
    RetractBins->usRetractCount = 15;

    PtrStatus.lppRetractBins = &RetractBins;//AQUI QUERO PASSAR

    std::cout << (*PtrStatus.lppRetractBins)->wRetractBin << std::endl; //lppRetractBins é um ponteiro para ponteiro, é necessário dereferencialo.

}
    
04.02.2017 / 14:29