-1

I set the array variable to static in modbusTCP.h.

static USHORT usRegInputBuf[REG_INPUT_NREGS];

After that, I include modbusTCP.h and use it in main.c.

usRegInputBuf[0] = 1;
usRegInputBuf[1] = 4; ...

But there is a problem that the variable value does not change.

pritnf("%u, %u\n", usRegInputBuf[0], usRegInputBuf[1]);

=> 0, 0

How can you solve the problem?

  • 3
    Welcome to SO. You show very little code. Please edit your question to include a [MCVE](https://stackoverflow.com/help/mcve). – Gerhardh Jul 13 '22 at 07:40
  • 5
    Are you doing the assignment and the printing in two different source files? Then its time for you to learn about the concept of [*translation units*](https://en.wikipedia.org/wiki/Translation_unit_(programming)) (commonly sabbreviated as TU) and about [*linkage*](https://en.cppreference.com/w/c/language/storage_duration#Linkage). A global variable declared `static` will have *internal* linkage, and only be defined in the current TU. If you define the variable in a header file, each TU that includes the header file will have their own private copy of the variable, not visible to any other TU. – Some programmer dude Jul 13 '22 at 07:41
  • 2
    As you have tagged the question with STM32 I assume your `int` variables are 32 bit and `USHORT` is 16 bit. That means `%u` is wrong format specifier. Use `%hu` instead. Also, you explicitely mention that you changed the array to `static`. Did it work before? – Gerhardh Jul 13 '22 at 07:42
  • Thanks for comments. when i use stm32cubeide, that code worked as I thought. and 1. Defining usRegInputBuf at modbusTCP.h 2. changed and printed usRegInputBuf at main.c So, the static variable declared in header cannot used in main?(because static variable was linked internally?) – 변상규 Jul 13 '22 at 08:00
  • 1
    If you make that `static` each C file will have its own instance of such a variable. You might reivisit what `static` means. Generally, headers should not define variables but only declare them. – Gerhardh Jul 13 '22 at 08:12
  • 1
    As others already commented, please learn about the difference of declaration and definition of variables. You cannot learn programming by accident. -- Just because one compiler-linker system works, it does not mean that your code is correct. In that IDE a linker option might be set that "merges" all the instances. – the busybee Jul 13 '22 at 08:19

1 Answers1

0
  1. First of all you need to cast. Embedded standard library ports usually do not support modifiers like 'h'. You are invoking UB
pritnf("%u, %u\n", (unsigned)usRegInputBuf[0], (unsigned)usRegInputBuf[1]);
  1. Secondly you will create a new array usRegInputBuf instance in every compilation unit where you include this header. Instead in the header you should only declare it as extern, and define in the C file where your modbusTCP code is located:

modbusTCP.h:

extern USHORT usRegInputBuf[REG_INPUT_NREGS];

modbusTCP.c

USHORT usRegInputBuf[REG_INPUT_NREGS];
0___________
  • 60,014
  • 4
  • 34
  • 74