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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:09:58+00:00 2026-05-24T16:09:58+00:00

So the following code compiles with no warnings etc with gcc, but for some

  • 0

So the following code compiles with no warnings etc with gcc, but for some reason, the swap code doesn’t actually modify the array by swapping the values…What could be going on here? One interesting thing is that tempalways contains what I want, it just doesn’t get used.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//STRUCTURES
struct complex_
{
  double re, im;
};

typedef struct complex_  complex;

//PROTOTYPES
int reverseBits(int x,int elements);
complex new_complex(double re, double im);

//MAIN
int main()
{
    int n,m,elements = 8;
    complex temp,**v;

    //Allocate memory for the struct array...
    v = malloc(sizeof(complex*));
    *v = malloc(sizeof(complex)*elements);

    //Initialize the struct array...
    for (n = 0; n < elements; n++)
    {
        (*v)[n] = new_complex(n,0);
    }

    //View the initialized struct array contents...
    for (n = 0; n < elements; n++){printf("%f+%fi\n", (*v)[n].re,(*v)[n].im);}

    //Swap elements for the bit reversal...
    for (n = 0; n < elements; n++)
    {
        m = reverseBits(n,elements);
        temp = (*v)[n];
        (*v)[n] = (*v)[m];
        (*v)[m] = temp;
    }

    //View the new swapped struct array contents...
    for (n = 0; n < elements; n++){printf("%f+%fi\n", (*v)[n].re,(*v)[n].im);}

    return 0;
}

//FUNCTION DEFINITIONS
int reverseBits(int x,int elements)
{
    //This function performs a binary bit reversal
    //for example 3 = 011 => 110 = 6...
    int num_bits = log2(elements);
    int reverse_x = 0;
    int i;

    for (i = 0; i < num_bits; i++)
    {
        if((x & (1 << i)))
            reverse_x |= 1 << ((num_bits - 1) - i);
    }
    return reverse_x;
}

complex new_complex(double re, double im)
{
    //This function creates a new complex number.
    complex r;
    r.re = re;
    r.im = im;
    return r;
}
  • 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-24T16:09:58+00:00Added an answer on May 24, 2026 at 4:09 pm

    If you swap all items of the array once with the item at the “reversed” index, then you’ll end up with the start state again. ie., for an array size of 8, these swaps are done :

    • swap item at index 0 with item at index 0
    • swap item at index 1 with item at index 4 (a)
    • swap item at index 2 with item at index 2
    • swap item at index 3 with item at index 6 (b)
    • swap item at index 4 with item at index 1 (a)
    • swap item at index 5 with item at index 5
    • swap item at index 6 with item at index 4 (b)
    • swap item at index 7 with item at index 7

    Notice that the swaps marked with the same letter ((a) or (b)) cancel each other out, and the others are no-ops.

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

Sidebar

Related Questions

The following code compiles in Visual C++ and gcc, but fails with Code Warrior
The following two code snippets compiles without any errors/warnings but while running it crashes.
The following code compiles without warnings under GCC 4.2, and as far as I
The following code compiles correctly under VC++ 8 on XPSP3, but running it causes
I have the following code which compiles with no warnings (-Wall -pedantic) with g++
I know unsigned int can't hold negative values. But the following code compiles without
The following code examples compiles but with a compiler warning class Animal{} class Dog
In Microsoft Oslo SDK CTP 2008 (using Intellipad) the following code compiles fine: module
I have the following code that compiles and works well: template<typename T> T GetGlobal(const
I came across the following code that compiles fine (using Visual Studio 2005): SomeObject

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.