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 6721901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:23:01+00:00 2026-05-26T09:23:01+00:00

I am writing this because I have really no idea why this is happening.

  • 0

I am writing this because I have really no idea why this is happening. I know how to correct it/go around it but what I would like to know is the reason this occurs. I am using C and my compiler is gcc 4.4.1 (TDM) running on an Intel machine.

Assumptions about floats: -Conform to the IEEE 754 standard
– Are stored in a Big Endian way

Let’s assume we have a function taking in an array of 4 bytes and returning them as a float. That is the goal of the function. Let’s also say that for example all the function will do is get the bytes in the “right order” and since the system is little endian it will just swap them and put them into the float to return a value. For simplicity’s sake I don’t include any checks for NaN or INF since this is not the purpose of this question.

float testFunction(char* arr)
{
    //this will be the float we return
    float ret;

    //let's just get a char pointer to the float so we can alter its byte values
    char* c = (char*)&ret;
    //just swap them so they conform with little endian byte order
    c[0] = arr[3];
    c[1] = arr[2];
    c[2] = arr[1];
    c[3] = arr[0];

    //up to here if you debug and watch ret's value it is correct as it is supposed to be
    return ret;
}

The problem is wherever I use the function … let’s say like below

   float f = testFunction(arr);

Then the float f has a completely irrelevant float value to the bytes you pass as parameters.

The way to succesfully go around this is to declare a function that accepts the float as a parameter and give it a value inside the function like so:

void testFunction(char* arr,float* f)
{
   char* c = ((char*)f)
   c[0] = arr[3];
   c[1] = arr[2];
   c[2] = arr[1];
   c[3] = arr[0];
 }

But still my question is, why does this happen when I try to return the value? I do understand that float ret is a temporary value inside the scope of the function but the return statement should copy its value outside of the function. Isn’t it correct?
What am I missing? I guess it must be something really obvious.

  • 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-26T09:23:02+00:00Added an answer on May 26, 2026 at 9:23 am

    I’ll say something stupid. But try initializing ret, like float ret = 0;.

    I’m not sure you can initialize a variable “piecemail” one char at a time and consider it to be “initialized” for the C standard (and the compiler)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(I'm writing this in the context of JavaScript, but will accept an algorithmically correct
I'm asking this because I'm in the process of writing two such editors for
I need a little help writing a query. I have this data... vDir iNumber
I have classes like this one: class SomeObject { public function __construct($param1, $param2) {
I've always been dabbling in javascript, but never really got hugely into writing a
I read this topic, but his problem maybe different from mine Writing to both
I'm not actually writing this software myself, but it occurred to me that I
I do really like key-chord.el but I'd need it to only trigger when I
I tried writing this class #include <memory> class ContainerUnique { public: ContainerUnique(void); ~ContainerUnique(void); private:
I've had trouble writing this code. I'm supposed to make a function that can

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.