How Can I Convert Struct?

How to convert c struct definitions to c++

  • iov[0] = (struct iovec) { .iov_base = &req, .iov_len = sizeof(req) }; I'm trying to convert a bit of code from the ss network utility to c++, and the above and similar keeps giving these errors: main.cpp|83|error: expected primary- expression before ‘struct’ main.cpp|83|error: expected ‘)’ before ‘struct’

  • Answer:

    C++ doesn't support that syntax. Try this: iovec iov[2]; iov[0].iov_base = &req; iov[0].iov_len = sizeof req; /* ... */ Or maybe: iovec iov[] = { { &req, sizeof req }, /* ... */ } What you were trying is a C99 feature called "compound literals". Also, the .iov_base thing is called a "designated initializer".

mpnordland at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

Compound literals are not present in C++. You can assign the struct members individually as cnicutar suggested or you can also define a temporary object and then assign it to iov first array element. Like this: iovec iov_temp = {&req, sizeof req}; iov[0] = iov_temp;

ouah

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.