0

main.c

#include <stdio.h>
#include "constants.h"

void main()
{
    printf("%hu", MOTORs_PINs.motor_right.terminal_1);
}

constants.c

#include "constants.h"

struct __MOTOR_PIN
{
    unsigned short int terminal_1;
    unsigned short int terminal_2;
};
struct __MOTORs_PIN
{
    struct __MOTOR_PIN motor_right;
    struct __MOTOR_PIN motor_left;
};

const __MOTORs_PIN MOTORs_PINs = {.motor_right.terminal_1 = 16,
                                  .motor_right.terminal_2 = 17,
                                  .motor_left.terminal_1 = 18,
                                  .motor_left.terminal_2 = 19};

constants.h

extern const struct __MOTORs_PIN MOTORs_PINs;

error

main.c: In function 'main':
main.c:7:30: error: invalid use of undefined type 'const struct __MOTORs_PIN'
    7 |     printf("%hu", MOTORs_PINs.motor_right.terminal_1);
      |                              ^

in the above-provided code, I'm getting error about invalid use of undefined type when I was using a constant nested struct.

Note: All files are in the same directory.

Actually, I want to keep all the constants of the project in the same file that's why I want it to work in a manner that whenever I use variable MOTORs_PINs.motor_right.terminal_1 its constant value i.e. 16 should be received.

Aman Rathore
  • 163
  • 9

0 Answers0