1

I am using struct with unsigned int bit-fields perfectly, but suddenly, after duplicating one of them, the compiler is losing its mind (it would seem). Here's my code:

typedef struct myStruct {
    unsigned int myVar:1;
} myStruct; // my compiler requires TWO declarations of the name for typedef

myStruct myNewStructVar;

myNewStructVar.myVar = 0; // throws error that "myNewStructVar" is unknown to the compiler

What gives? Again, I have two versions of this EXACT thing and it works fine.

5
  • Your code works fine in MSVS2010. what your compiler? Commented Nov 28, 2013 at 2:01
  • C89 for a microchip, same error in Xcode using the C89. Xcode throws "unknown type name" error. Commented Nov 28, 2013 at 3:52
  • So maybe someone can answer this, it wasn't allowing me to do this in the variable declaration area OUTSIDE of a method. However, inside a method it worked fine. Geesh! Commented Nov 28, 2013 at 4:16
  • "method"? Are you speaking C, C++ or something else? Commented Nov 28, 2013 at 5:07
  • Sorry, I'm doing C not C++. It's standard to not assign values outside of a method using dot notation. I'm spoiled by ++. :) Commented Nov 29, 2013 at 5:10

1 Answer 1

1

You can declare the variable as a global outside of function scope, but you can't have a separate line of code to set its value. Setting the value on a separate line is executable code rather than initialization, and is not permitted there.

If you want to initialize it at the point of declaration, try:

myStruct myNewStructVar = {0};

This should work where it is now.

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

Comments

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.