Tan Dat
Tan Dat

Reputation: 3117

swap with non-const reference parameters

I got [Error] invalid initialization of non-const reference of type 'float&' from an rvalue of type 'float'

#include <stdio.h>
void swap(float &a, float &b){
    float temp=a;
    a=b;
    b=temp;
}
main()
{
    int a=10, b=5;
    swap((float)a, (float)b);
    printf("%d%d",a,b);
}

Upvotes: 2

Views: 559

Answers (4)

cdhowie
cdhowie

Reputation: 168988

In C++ you should be using std::swap, or if you prefer you can write your own template that will swap any two values.

template <typename T>
void swap(T & a, T & b)
{
    T temp = a;
    a = b;
    b = temp;
}

int main() {
    int a = 10, b = 5;
    swap(a, b);
    std::cout << a << " \t " << b << std::endl;
    return 0;
}

What you are trying to accomplish (treating an int as a float) is going to result in undefined behavior -- it might work on your compiler but it could easily break on a different compiler or architecture. You can use reinterpret_cast to force this behavior if you really want to.

Upvotes: 0

David C. Rankin
David C. Rankin

Reputation: 84551

Vlad is correct, why cast to float? Use int for all values. However, if you have some reason for doing it that way, you must be consistent in your cast and references:

#include <stdio.h>

void swap(float *a, float *b){
    float temp=*a;
    *a=*b;
    *b=temp;
}

int main()
{
    int a=10, b=5;
    swap((float*)&a, (float*)&b);
    printf("\n%d%d\n\n",a,b);
    return 0;
}

output:

$ ./bin/floatcast

510

When you pass an address to a function, it must take a pointer as an argument. Thus void swap(float *a,.. When you need a reference to an address of a variable (to pass as a pointer), you use the address of operator &. When you handle values passed as a pointer, in order to operate on the values pointed to by the pointer you must dereference the pointer using the * operator. Putting all that together, you get your code above. (much easier to just use int... :)


C++ Refernce

If I understand what you want in your comment, you want something like this:

#include <iostream>

// using namespace std;

void swap(float& a, float& b){
    float temp=a;
    a=b;
    b=temp;
}

int main()
{
    int a=10, b=5;
    swap ((float&)a, (float&)b);
    std::cout << std::endl << a << b << std::endl << std::endl;
    return 0;
}

output:

$ ./bin/floatref

510

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

You are trying to swap temporary objects (created due to using the casting). that moreover would be deleted after exiting from the swap. You may not bind temporary objects with non-constant references. So there is no sense in such a call. It is entire unclear why you are trying to cast the both integers to float that to swap them. Why do not swap integers themselves?

Write the function like

void swap( int &a, int &b )
{
    int temp = a;
    a = b;
    b = temp;
}

Take into account that there is already standard function std::swap

If you want to write swap function in C then it will look like

void swap( int *a, int *b )
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

Upvotes: 1

Karthikeyan.R.S
Karthikeyan.R.S

Reputation: 4041

You have to get the array using the pointers(*). While passing only you have to give the ampersand(&). Use this following code.

#include <stdio.h>
void swap(int* a, int *b){
float temp=*a;
*a=*b;
*b=temp;
}
main()
{
 int a=10, b=5;
 swap(&a, &b);
 printf("%d \t %d\n",a,b);
}

Upvotes: 0

Related Questions