Is it possible to create 'empty' members within a struct in C?

Related searches

I'm tryin to create a few array-like structures for handeling MODBUS data & adressing.

I'm doing it within a structure so it's simpler later on to set certain values instead of referencing a datasheet to find out which address is coupled to the data.

Some addresses are left blank and are reserved for future use (I know this isn't specified in the MODBUS standard, the data shouldn't have to be exactly MODBUS specific).

I've created a struct which compiles on the software for the embedded device this should run on. However I wasn't quite sure it would run the way I imagined it, so I ran some test code on 2 different compiler. 1 being an online c-compiler which runs the way I excepected it to with just one warning at the reserved byte. The 2nd one is ran within a visual studio c++ console application, which won't run cause it generates the error:

Error C2208 'unsigned short': no members defined using this type

This is the complete code which should run:

#include <stdio.h>
#include <stdint.h>
#define U16 uint16_t
#define UI  unsigned int
struct t {
    U16 t1;
    union {
        struct {
            UI b_f1 : 2;
            UI b_f2 : 2;
            UI b_f3 : 2;
            UI b_f4 : 2;
            UI b_f5 : 2;
            UI b_f6 : 2;
            UI b_f7 : 2;
            UI b_f8 : 2;
        }f;
        U16 c;
    }t2;
    U16; //Reserved space for later use <- this gives me the compiler error
    U16 t3;
}test;

int main()
{
    test.t3 = 12;
    test.t2.f.b_f1 = 0; //0
    test.t2.f.b_f2 = 2; //4 (8)
    test.t2.f.b_f3 = 0; //16
    test.t2.f.b_f4 = 0; //64
    test.t2.f.b_f5 = 0; //256
    test.t2.f.b_f6 = 0; //1024
    test.t2.f.b_f7 = 2; //4096 (8192)
    test.t2.f.b_f8 = 0; //16384
    //tot: 8200    
    test.t1 = 3000;
    int h; //test variable
    for (int i = 0; i < (sizeof(test) / sizeof(U16)); i++) {
        h = *(&test.t1 + (i*sizeof(U16));

        printf("%d", h); //here I can now send every MODBUS address & data
    }

    return 0;
}

Setting the variables is not neccesarily needed, it's just for testing purposes

I excpected it will just generate a structure of 4 elements, one being a 0 pointer, that way the addressing is correct in a way that address 3 is not used and therefore 0 while address 4(t3) will contain more data.

Is there a way to make this work I planned to do, or should I just create some variables in between called Reserved1..Reserverd2..etc ?

A "declaration" like

U16; //Reserved space for later use

is simply not valid.

Just use some dummy name to solve your problem, like e.g.

U16 _reserved_; //Reserved space for later use

Is it possible to create life from scratch?, Now it might not be the same thing as creating life in a test tube or in the ways of a god, but it is one from of life being created normally. If cells can be synthetically � Scientists fear that it may prove impossible to produce a working coronavirus vaccine and believe the world may have to simply learn to adapt to the permanent threat of COVID-19.. The UK's Chief

No you can't. A type on its own is not a valid declaration within a structure definition.

What you could do is use __LINE__ to help guarantee that the name you assign to the variable is unique:

#define _concat(a,b) a##b
#define concat(a,b) _concat(a,b)

and write

U16 concat(t, __LINE__);

which is unique insofar that the struct name t is unique.

Is it possible to create a perfect vacuum?, page: https://www.patreon.com/teded View full lesson: https://ed.ted.com/ lessons/is-it Duration: 4:32 Posted: Sep 12, 2017 Now that it’s possible to add components to the liposome bubbles without popping them, groups can plan how to make molecules work together. Almost anything life-like requires cellular energy

Is there a way to make this work I planned to do?

No.

or should I just create some variables in between called Reserved1..Reserverd2..etc

Yes.

Is it possible to create an artificial atmosphere?, YES OFF COURSE , as you can build a satelite station you can create atmosphere , its so plain ,but costs . the biosphere built in America -where scientists are� Yes. It is possible, If you don't make your account with your real name. I think getting into trouble has nothing to do with having two accounts. Any account that you have can be locked out of the sudden and for no reason.

Is it Possible to Create a Website Without Buying a Domain Name , This article attempts to answer this, and on the way, tries to clarify a misunderstanding about creating websites that usually accompanies such a� The best photo book maker websites and apps, like Artifact Uprising, Walmart, Google Photos, and more, that ship photo books to you or let you pick up in stores.

Is it possible to create zero waste?, This content requires a special license. Please sign in or create an account to see if you have access to this content. Sign in. Join. Is it possible to create an absolute vacuum? It is an interesting speculation, and I have enjoyed reading contributors who have posted on this thread. Absolute vacuums undoubtedly have a profound impact on the behavior of the universe, and I believe that in the not-too-far future it will be possible that absolute vacuums will transition from

Build Your Own AI (Artificial Intelligence) Assistant 101: Remember the time, when you were watching Iron Man and wondered to yourself, how cool it would be if you had your own J.A.R.V.I.S? Well, It's time to make that dream into a reality. Artificial intelligence is the next gen. Imagine how cool it woul

Comments
  • Just what I was scared of, is it possible to use such a variable multiple times?
  • @user3005497 Then you have to use different names (e.g. _reserved_1_, _reserved_2_, etc.) . Or, if the data is consecutive, use a byte array instead.
  • A type followed by a semicolon is normally a valid declaration, but am not sure if it is valid inside a struct definition.
  • @machine_1: I'm not enough of a C expert to make that call, so I've cheated and amended the answer.
  • Indeed, in C11 it is valid yet gcc emits a warning but it is invalid in C++11.
  • If doing dirty, do it right, so it survives living in global scope, concat __FILE__ as well. ;)
  • Ah dxxm. A pity #define . _ does not work as well.