Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 764627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:45:33+00:00 2026-05-14T16:45:33+00:00

I am working on a C program that uses a Union. The union definition

  • 0

I am working on a C program that uses a Union. The union definition is in FILE_A header file and looks like this…

// FILE_A.h****************************************************
xdata union  
{
long position;
char bytes[4];
}CurrentPosition;

If I set the value of CurrentPosition.position in FILE_A.c and then call a function in FILE_B.c that uses the union, the data in the union is back to Zero. This is demonstrated below.

// FILE_A.c****************************************************
int main.c(void)
{
    CurrentPosition.position = 12345;
    SomeFunctionInFileB();
}

// FILE_B.c****************************************************
void SomeFunctionInFileB(void)
{
    // After the following lines execute I see all zeros in the flash memory.
    WriteByteToFlash(CurrentPosition.bytes[0];
    WriteByteToFlash(CurrentPosition.bytes[1];
    WriteByteToFlash(CurrentPosition.bytes[2];
    WriteByteToFlash(CurrentPosition.bytes[3];
}

Now, If I pass a long to SomeFunctionInFileB(long temp) and then store it into CurrentPosition.bytes within that function, and finally call WriteBytesToFlash(CurrentPosition.bytes[n]… it works just fine.

It appears as though the CurrentPosition Union is not global. So I tried changing the union definition in the header file to include the extern keyword like this…

extern xdata union  
{
long position;
char bytes[4];
}CurrentPosition;

and then putting this in the source (.c) file…

xdata union  
{
    long position;
    char bytes[4];
}CurrentPosition;

but this causes a compile error that says:

C:\SiLabs\Optec Programs\AgosRot\MotionControl.c:76: error 91: extern definition for 'CurrentPosition' mismatches with declaration.
C:\SiLabs\Optec Programs\AgosRot\/MotionControl.h:48: error 177: previously defined here

So what am I doing wrong? How do I make the union global?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T16:45:34+00:00Added an answer on May 14, 2026 at 4:45 pm

    Is FILE_A.h really MotionControl.h? If so I think the fix is to define a union type in the header:

    typedef
    union xdata
    {
        long position;
        char bytes[4];
    } xdata;
    

    And declare a global variable of that type elsewhere in a header file (maybe the same one):

    extern xdata CurrentPosition;   // in a header file
    

    Finally define the global variable in a C file exactly once. Maybe in file_a.c:

    xdata CurrentPosition;
    

    Of course a better fix might be to pass the xdata variable you want to write out to flash to SomeFunctionInFileB() so you don’t have to depend on a global variable, which are well known to be problematic when not very, very carefully used. And there seems to be no good reason to not pass the data as a parameter:

    // in a header file
    void SomeFunctionInFileB( xdata const* pPosition);
    
    
    void SomeFunctionInFileB( xdata const* pPosition)
    {
        // After the following lines execute I see all zeros in the flash memory.
        WriteByteToFlash(pPosition->bytes[0];
        WriteByteToFlash(pPosition->bytes[1];
        WriteByteToFlash(pPosition->bytes[2];
        WriteByteToFlash(pPosition->bytes[3];
    }
    

    And call it like so:

    int main.c(void)
    {
        CurrentPosition.position = 12345;
        SomeFunctionInFileB( &CurrentPosition);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a reliable file transfer program that uses UDP. (for a
I'm working on a program that uses PHP's internal array pointers to iterate along
I'm working on a program that searches entire drives for a given file. At
I am working on a program that encodes a file based on a supplied
I am working on a background program that will be running for a long
Working with a program that uses 16bytes 4v4 one byte matrices : unsigned char
I'm working with a program that uses two-dimensional arrays of Strings (probably not that
I am working on a program that uses Spring and obtains Hibernate transactions transparently
I am working on a program that uses IE to display its help pages.
I have been working on a Android Web Services program that uses a number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.