2

I'm trying to create a structure which contains multiple function pointers, however when I try to create an instance of the structure I get a error "variable "stCmdTable" was declared with a never-completed type".

I have a header file in which I have the following code:

typedef int (*pStCmd) (void);

struct stCmdStruc {
  pStCmd id;
  pStCmd measure;
  pStCmd setRelay;
};

typedef struct stCmdStruct stCmdStruct;

stCmdStruct stCmdTable;

I want to create stCmdTable and assign functions to all the function pointers in the stCmdTable, but when it doesn't like my declaration of stCmdTable.

I've also tried doing something like this, where I try to initialise all the function pointers to functions straight of the bat with my structure defintion, but it really doesn't like this telling me expected a ";" at the end of each line in the struct.

typedef int (*pStCmd) (void);

struct stCmdStruc {
  pStCmd id = sendId2;
  pStCmd measure = sendMeasurement2;
  pStCmd setRelay = setRelay2;
};

typedef struct stCmdStruct stCmdStruct;

stCmdStruct stCmdTable;

Can anyone please shed some light on what I'm doing wrong?

5
  • 5
    struct stCmdStruc != struct stCmdStruct. You typo'd Commented Oct 30, 2018 at 11:07
  • Please edit your question to include a Minimal, Complete, and Verifiable Example. And then also include a copy-paste (as text) of the full and complete error output from the example, including any possible informational notes. Commented Oct 30, 2018 at 11:11
  • 1
    Please ask only one question in the question. Your second question is separate issue and should be asked separately. Commented Oct 30, 2018 at 11:13
  • C++ automatically introduces the structure name as a type name; C does not. Either use struct before stCmdStruct or add typedef struct stCmdStruct stCmdStruct;. Commented Oct 30, 2018 at 11:22
  • “C++ ...” yah, that modicum of convenience is like the free hit of heroin; what could go wrong if you try just a bit more.... Commented Oct 30, 2018 at 12:24

1 Answer 1

1

Try this . . .

typedef int (*pStCmd_Type) (void);

typedef struct _stCmdStruct {
  pStCmd_Type id;
  pStCmd_Type measure;
  pStCmd_Type setRelay;
} stCmdStruct_Type;

stCmdStruct_Type stCmdTable[your_table_size];

stCmdTable[0].id = sendId2;
stCmdTable[0].measure = sendMeasurement2;
stCmdTable[0].setRelay = setRelay2;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.