-1

I want to change one of attribute in my simple struct. I can't change anything in main function. But the compiler giving me error about scalar type - what does it exactly mean and what do i wrong?

#include <iostream>

using namespace std;

struct Number{
    int a;
    double b;
};

double zmiana(Number *number,double scale){
    number->a*=scale;
    return number->a;
}

int main()
{
    Number number1={2,3.14};
    Number number2=zmiana(&number1,2.);
    cout<<&number2;
    return 0;
}

Expected output: 4 3.14

kiner_shah
  • 3,939
  • 7
  • 23
  • 37
  • 5
    `zmiana` returns a `double` but you're assigning it to a `Number`. That conversion isn't defined. – perivesta Nov 16 '21 at 09:59
  • What do you expect to be in `number2`? `cout<<&number2` prints the memory address of `number2`, what do you expect to achieve? – Jabberwocky Nov 16 '21 at 10:04

1 Answers1

0

Here is the potential fix:

#include <iostream>


struct Number
{
    int a;
    double b;
};

double zmiana( Number* number, double scale )
{
    number->a *= scale;

    return number->a;
}

int main()
{
    Number number1 = { 2, 3.14 };
    Number number2;
    number2.b = zmiana( &number1, 2. );
    std::cout << &number2; // Notice that what you do here is printing a pointer's value

    return 0;
}
digito_evo
  • 3,216
  • 2
  • 14
  • 42