0

When initializing a struct using curly braces, it does not seem to work with an array of chars. I can write an equivalent constructor that works below. Is there any syntax so I don't have to write the constructor?

#include <cstdint>
#include <cstring>
using namespace std;

struct NamedLocationData {
  uint16_t offset;
  char stateName[21];
  float lat;
  float lon;
  uint32_t population;

    NamedLocationData(uint16_t offset, const char stateName[21],
                                        float lat, float lon, uint32_t population)
        :   offset(offset), lat(lat), lon(lon), population(population) {
        strncpy(this->stateName, stateName, 21);
    }

};

int main() {
    uint16_t nameOffset = 0;
    char stateName[21] = "New York";
    float lat = 40;
    float lon = -74;
    uint32_t population = 8000000;

    #if 0
    NamedLocationData temp = NamedLocationData
        {
         nameOffset, stateName, lat, lon, population
        };
    #endif
    
    NamedLocationData temp( nameOffset, stateName, lat, lon, population);
        
}
3
  • char stateName[21]; -> std::string stateName; Commented Aug 10, 2022 at 20:52
  • 1
    Arrays are really simple, their behaviour defined in the 1970s when computers had less processing power and RAM than a Dorito. You can't initialize them with a variable, only a brace-enclosed list or a string literal in the case of a char array, so : offset(offset), stateName(stateName), lat(lat), lon(lon), population(population) cannot work the way you want. Commented Aug 10, 2022 at 20:58
  • 2
    Change C-era char stateName[21] to the magic of the C++-era std::array<char, 21> stateName. Commented Aug 10, 2022 at 21:18

1 Answer 1

1

Default constructors are one of the special member functions. If no constructors are declared in a class, the compiler provides an implicit inline default constructor.

I suggest you change char[] to string so that stateName will be able to get a value.

#include <cstdint>
#include <cstring>
#include <string>
using namespace std;
struct NamedLocationData {
    uint16_t offset;
    string stateName;
    float lat;
    float lon;
    uint32_t population;

    NamedLocationData(uint16_t offset, string stateName,
        float lat, float lon, uint32_t population)
        : offset(offset), lat(lat), lon(lon), population(population) ,
        stateName(stateName){}


};

int main() {
    uint16_t nameOffset = 0;
    string stateName = "New York";
    float lat = 40;
    float lon = -74;
    uint32_t population = 8000000;

#if 0
    NamedLocationData temp = NamedLocationData
    {
     nameOffset, stateName, lat, lon, population
    };
#endif
    NamedLocationData temp(nameOffset, stateName, lat, lon, population);

    return 0;
}

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

avoiding string for efficiency reasons.

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.