简体   繁体   中英

Array of Structs with an enum member causes error when giving the array a NULL termination, but no error when not doing so

I've tried searching for this, but I've not found what causes this strange behavior, there must be something I'm missing but I can't tell what it is:

The problem is the following i have the following struct:

struct s_log_message{
    lll level;
    const unsigned int i;
    const char *msg;
}typedef log_message;

and the following array of said structs:

const log_message logging_messages[]{
    {
        error,
        0,
        ACR "ERROR" ACS ":" "No arguments given, use -h or --help for information on how to use the program\0"
    },
    NULL
};

ACR and ACS are defines to change terminal color.

lll level is an enum:

enum log_level_list{none=0,info=1,warn=2,error=3};
typedef enum log_level_list     lll;

All this code is in 3 different files:

log.h:

#ifndef LOG
#define LOG 220

enum log_level_list{none=0,info=1,warn=2,error=3};
typedef enum log_level_list     lll;

class log{
public:
    static void                         set_log_level(lll val);
    static unsigned char        get_log_level(void);

    static void                         l(unsigned int warn_i);
private:
    log(){}

    static unsigned char        log_level;
};

#endif

lmg.h:

#ifndef     LOG_MESSAGES
#define     LOG_MESSAGES    100
#include "../lib/log.h" //included for definition of enum type lll

struct s_log_message{
    lll level;
    const unsigned int i;
    const char *msg;
}typedef log_message;

extern const log_message logging_messages[];

#endif

and lmg.cpp:

#include "../lib/col.h"         //terminal color defines
#include "../lib/lmg.h"         //header for this file
#include <cstddef>                  //included for definition of NULL

const log_message logging_messages[]{
    {
        error,
        0,
        ACR "ERROR" ACS ":" "No arguments given, use -h or --help for information on how to use the program\0"
    },
    NULL
};

To recap the problem is that terminating the loggin_messages[] array with NULL causes the compiler o throw the following error:

g++ -Wall -o DFHM ./src/main.cpp ./lib/lib.cpp ./lib/EasyBMP.cpp ./lib/dat.cpp ./lib/log.cpp ./lib/lmg.cpp
./lib/lmg.cpp:12:1: error: invalid conversion from ‘long int’ to ‘lll’ {aka ‘log_level_list’} [-fpermissive]
   12 | };
      | ^
      | |
      | long int
make: *** [makefile:14: main] Error 1

I'm aware that spreading so little code among so many files is counterproductive, I'm just trying to get used to doing that so that if ever i get into larger projects I'll be accustomed, and to practice of course.

You don't need to terminate every kind of array with NULL. In fact, strictly speaking not even char arrays need to be terminated with NULL. You can happily have non-NULL terminated arrays as long as you write your code so that you don't go beyond the end of the array.

Of course with char arrays there is an established convention such that many functions interpret a NULL valued char as the end of useful data. However that idea isn't established for any other type of array.

Or to say it a different way, NULL isn't a valid value for a struct (or a class , etc). While it is for a pointer to one, that's a completely different type. When you have an actual instance of a thing (such as a struct ) it cannot be NULL.

NULL is a null pointer constant. It's intended to set pointers to a null, not-pointing-to-anything, value.

logging_messages doesn't hold pointers though. It's an array of log_message objects. It doesn't make any sense to initialize a log_message to NULL .

If you want to add an "empty" object to serve as a terminator to your array instead of storing the array's size separately, you can do that, but your type's "empty" state is up to you. For instance { none, 0, nullptr } could be your defined "empty" state. That particular state could also be written as simply {} , since members that aren't given in aggregate initialization will be value-initialized.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM