0

I have defined this struct:

typedef struct WHEATHER_STRUCT
{
   unsigned char packetID[1];
   unsigned char packetSize[2];
   unsigned char subPacketID[1];
   unsigned char subPacketOffset[2];
   ...
} wheather_struct;

How can I initialize this struct (using constructor or new) accessing by an attribute name? For example:

wheather_struct.packetID = 1;

Finally

I tried this solution and it works for me, but do you think it is a good choice?

WHEATHER_STRUCT * wheather_struct = new WHEATHER_STRUCT();
*weather_struct->packetID = '1';

And for a float attribute:

wheather_struct->floatAttribute= 111.111
13
  • 4
    Give the struct a proper constructor (or use in-class initializers for the default values) and stop using malloc in C++. Commented Sep 26, 2018 at 15:26
  • 5
    "How can I initialize this struct using malloc and update this value accessing to an attribute name?" Why do you even use malloc? What's wrong with using constructors, and new? Commented Sep 26, 2018 at 15:26
  • 5
    Why are you using malloc in a C++ program? If any of those members of WEATHER_STRUCT change to non-POD types, that malloc call will not create the object and you now have to hunt down a hard-to-find bug. Commented Sep 26, 2018 at 15:27
  • Ok, I can not use new because the stuct is so long. But this I used malloc, but now I know that is not a good choise. Should I use constructor for this example? Commented Sep 26, 2018 at 15:33
  • Ok, I can not use new because the stuct is so long. -- ???????? Do you know what new does? Commented Sep 26, 2018 at 15:34

2 Answers 2

1

In C++ you can use new to allocate and initialize:

wheather_struct *p = new wheather_struct();

Note the parenthesis at the end - this is value initialization - fills members of built-in types with 0.

And then:

p->packetID[0] = 1;
Sign up to request clarification or add additional context in comments.

8 Comments

If I use this solution compiler says me that "expression must be an L-value modifiable"
@JoseJimRin On which line?
It seems like i need to write *p -> packetID = '1', this works for me.
@JoseJimRin p->packetID[0] = 1;
OK, thanks, But if I have 2 bytes to represent an information. And I need to put a value 300. *p -> packetID = '300'; Is the correct option?
|
1

You can add initializers {} to your arrays to initialize them to zeros like this:

struct wheather_struct // no need for typedef in C++
{
   unsigned char packetID[1]{};
   unsigned char packetSize[2]{};
   unsigned char subPacketID[1]{};
   unsigned char subPacketOffset[2]{};
};

Then use new to dynamically create an object:

auto w = new weather_struct;

w->packetID[0] = 'A';

Don't forget to delete it later:

delete w;

But (much better) use a smart pointer:

auto w = std::make_unique<weather_struct>(); // will delete itself

w->packetID[0] = 'A';

// no need to delete it

Or (even better) just use it as a value object (not always possible):

weather_struct w; 

w.packetID[0] = 'A';

4 Comments

Compiler do not let me to put brackets {} in the struct definition.
@JoseJimRin You probably need to set the C version on your compiler to C++11 or above. What compiler are you using?
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.XX.YYYYY.ZZ for 80x86
@JoseJimRin I think you can use /std:c++11 or /std:c++latest on the command line.

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.