Pass a struct as parameter

Asked

Viewed 253 times

0

I need that struct

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

Turn a parameter I’ll put here.

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

How do I do ?

I’m trying to do it like this:

WFSPTRSTATUS PtrStatus;
LPWFSPTRRETRACTBINS RetractBins;

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

PtrStatus.lppRetractBins = &RetractBins;//AQUI QUERO PASSAR
  • There is still no context to what you are doing. This is not working?

  • It n accuses build error, but I know it doesn’t work.

  • 1

    Ask the question the error. Give full information so we can help.

1 answer

1


No object has been created and you are modifying the members of the struct by a pointer that points to nothing. This is undefined behavior.

The mistake is in:

LPWFSPTRRETRACTBINS RetractBins;

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

Here RetractBins is a pointer _wfs_ptr_retract_bins pointing to no object.

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

The correct code would be: (unused 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.

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.