What happened in the function convert(&m)? [duplicate] - c++

This question already has answers here:
Post-increment and Pre-increment concept?
(14 answers)
Closed 1 year ago.
Here is the code:
int convert(int* a) {
return (*a)++;
}
int main(){
int m = 56;
int n = convert(&m);
cout << m << endl;
m = convert(&m);
cout << m << endl;
return 0;
}
Why is the answer m=57 instead of m=58 after m=convert(&m)?

The second call increments m to 58, but it returns the original value (57) due to the use of the post-increment ++ operator. That original value is then assigned back to m, overwriting the incremented value. The net effect is that m is unchanged.
You can verify this by adding some printouts to see the exact values in play:
int convert(int* a) {
std::cout << "*a was " << *a << std::endl;
int ret = (*a)++;
std::cout << "*a is now " << *a << std::endl;
std::cout << "return value is " << ret << std::endl;
return ret;
}
m = convert(&m); prints:
*a was 57
*a is now 58
return value is 57

Related

variable in c++ is changing in a function [duplicate]

This question already has answers here:
Restore the state of std::cout after manipulating it
(9 answers)
Closed last year.
I have a simple converting function however the variable is changing.
void convert_print(std::string type,int num)
{
if(type=="oct"){
std::cout << num << " oct value is: " << std::oct << num << endl;
}
else if(type == "hex"){
std::cout << num << " hex value is: " << std::hex << num << endl;
}
}
int main()
{
int num = 30;
convert_print("hex",num);
convert_print("oct",num);
}
Prints
30 hex value is: 1e
1e value is: 30
Why is num getting changed to 1e when I call the function again. I never reassigned num
You haven't told cout to reset the formatting after your last call. This question describes how to restore it. Restore the state of std::cout after manipulating it

c++ return to a different result

I found that the parameter I returned is different in the outer function when it's called than where it was in the local function. I set some printing out in each section of the code but still have no idea why the parameter i is not returned:
int assign_cookie(vector<int>& g, vector<int>& s){
if(s.size()==0 || g.size()==0){
cout << "size = 0, returning 0" << endl;
return 0;
}
cout << "s.size() = " << s.size() << endl;
cout << "g.size() = " << g.size() << endl;
sort(g.begin(), g.end());
sort(s.begin(), s.end());
unsigned int i, j = 0;
while(i<g.size() && j<s.size()){
if(g[i]<=s[j]) i++;
j++;
}
cout << "(assign_cookie) i = " << i << endl;
return i;
}
when I called it in the main function as follows:
int main(){
int g[] = {1,2,3};
int s[] = {1,1};
vector<int> vg (g, g+3);
vector<int> vs (s, s+2);
int result = assign_cookie(vg,vs);
cout << "result = " << result << endl;
}
I got results as follows:
s.size() = 2
g.size() = 3
(assign_cookie) i = 1
(main) result = 0
I was very confused with the result. Anyone has an idea what's going on?
Thanks in advance for your help!
You have undefined behaviour, because here
unsigned int i,j=0;
you only initialize j but not i and then you use i as index in g[i] without ever assigning it a meaningful value. Hence it is UB and anything can happen. Actually this is an interesting case of UB, as when you do
unsigned int i,j=0;
std::cout << "i = " << i << "\n";
it still prints
i = 0
(assign_cookie) i = 1
result = 0
see also here. Even if the compiler seems to initialize i to 0 it still realizes that there is UB and takes the freedom to produce garbage output.

C++ strange behaviour of cout's flush. [duplicate]

This question already has answers here:
Order of execution in operator <<
(4 answers)
Closed 6 years ago.
Consider the following code. expected output should be
0 1
1 2
2 3
and so on.
#include<iostream>
using namespace std;
int f=0;
int B()
{
return f;
}
int A()
{
return f++;
}
int main()
{
cout<<A()<<" "<<B()<<endl;
cout<<A()<<" "<<B()<<endl;
cout<<A()<<" "<<B()<<endl;
cout<<A()<<" "<<B()<<endl;
return 0;
}
but the actual output is
0 0
1 1
2 2
and so on.. why?
and if i change code like this-
int main()
{
int f=0;
cout<<f++<<" "<<f<<endl;
cout<<f++<<" "<<f<<endl;
cout<<f++<<" "<<f<<endl;
cout<<f++<<" "<<f<<endl;
return 0;
}
then i get correct expected output
why?
The order of evaluation of the operands of << is not specified. So
cout << A() << " " << B() << endl;
can be treated as either:
temp1 = A();
temp2 = B();
cout << temp1 << " " << temp2 << endl;
or as:
temp2 = B();
temp1 = A();
cout << temp1 << " " << temp2 << endl;
Performing side effects on a variable and accessing it without defined sequencing results in undefined behavior.

Why doesn't my function output what's expected [duplicate]

This question already has answers here:
What is a reference variable in C++?
(12 answers)
Closed 8 years ago.
Hi I'm currently learning C++ and I'm trying to pass the value by reference, but I'm having issues with getting the correct output. What seems to be the problem??
void ref(int a)
{
cout << "a = " << a << endl;
a = 1;
cout << "a = " << a << endl;
}
int main()
{
int b = 10;
cout << "b = " << b << endl;
ref(b);
cout << "b = " << b << endl;
return 0;
}
Unless you put void ref(int &a){} you are not actually changing the value of a.
For pass by reference you have to use:-
void ref(int& a) {}

Address of function pointers in C++ [duplicate]

This question already has answers here:
How to print function pointers with cout?
(7 answers)
Closed 9 years ago.
I'm not clear on what the values that are being returning from calling:
&next, fp, *fp, &return_func_ptr, fp_ptr, &fp_ptr, *fp_ptr
They all seem to give me the value 1. What does it mean?
Also, how would I declare
int (*return_f())(char)
to receive a parameter without using typedef?
#include <iostream>
int next(int n){
return n+99;
}
// returns pointer to a function
typedef int (*fptr)(int); // using typdef
fptr return_func_ptr(){
return next;
}
int f(char){
return 0;
}
int (*return_f())(char){ // how do you pass a parameter here?
// std::cout << "do something with " << param << std::endl;
return f;
}
int main()
{
int x = 5;
// p points to x
int *p = &x;
std::cout << "x=" << x << std::endl; // 5, value of x
std::cout << "&x=" << &x << std::endl; // 0x7fff6447a82c, address of x
std::cout << "p=" << p << std::endl; // 0x7fff6447a82c, value of p is address of x
std::cout << "*p=" << *p << std::endl; // 5, value of x (p dereferenced)
std::cout << "&p=" << &p << std::endl; // 0x7fff6447a820, address of p pointer
// change value of x thru p
// p = 6; // error, can't set int* to int
*p = 6;
std::cout << "x=" << x << std::endl; // 6
int y = 2;
// int *q = y; // error can't initiate with type int, needs int*
// pointer to a function
int (*fp)(int);
std::cout << "&fp=" << &fp << std::endl; // 0x7fff66da6810, address of pointer fp
std::cout << "fp=" << fp << std::endl; // 0, value of pointer fp
fp = &next; // fp points to function next(int)
fp = next;
std::cout << "&next=" << &next << std::endl; // 1, address of function?
std::cout << "fp=" << fp << std::endl; // 1, value is address of function?
std::cout << "&fp=" << &fp << std::endl; // 0x7fff66da6810, address of pointer fp?
std::cout << "*fp=" << *fp << std::endl; // 1, address of function?
// calling function thru pointer
int i = 0;
i = (*fp)(i);
std::cout << "i=" << i << std::endl; // 99
i = fp(i);
std::cout << "i=" << i << std::endl; // 198
// function returns pointer to function
fptr fp_ptr = return_func_ptr();
std::cout << "&return_func_ptr=" << &return_func_ptr << std::endl; // 1
std::cout << "fp_ptr=" << *fp_ptr << std::endl; // 1
std::cout << "&fp_ptr=" << *fp_ptr << std::endl; // 1
std::cout << "*fp_ptr=" << *fp_ptr << std::endl; // 1
int j = fp_ptr(1);
std::cout << "j=" << j << std::endl; // 100
}
There is some pointer here who seems not clear :
// pointer to a function
int (*fp)(int);
std::cout << "&fp=" << &fp << std::endl; // 0x7fff66da6810, address of pointer fp
std::cout << "fp=" << fp << std::endl; // 0, value of pointer fp
Here fp is undefined. Those lines have an undefined behaviour.
After that :
// function returns pointer to function
fptr fp_ptr = return_func_ptr();
std::cout << "&return_func_ptr=" << &return_func_ptr << std::endl; // 1
std::cout << "fp_ptr=" << *fp_ptr << std::endl; // 1
std::cout << "&fp_ptr=" << *fp_ptr << std::endl; // 1
// ^^^^^^^^^^ ^^^^^^^
std::cout << "*fp_ptr=" << *fp_ptr << std::endl; // 1
There are two things here :
On the line I pointed, I'm not sure it is what you wanted to test.
Also, cout doesn't have an overload to take a function pointer, it will take a bool instead. So it should be :
std::cout << "fn_ptr=" << reinterpret_cast<void*>( fn_ptr ) << std::endl;
I would suggest you to read this article about function pointer, it explains almost all you need to know : http://www.learncpp.com/cpp-tutorial/78-function-pointers/
std::cout << "fp_ptr=" << *fp_ptr << std::endl;
should be
std::cout << "fp_ptr=" << (void*)fp_ptr << std::endl;
The cout operator doesn't have an overload for a function pointer, so it uses bool instead. That's why you always get 1 as output. When I compile your code, I even get a warning for that, telling me that it will always evaluate to true. You should switch on all warnings and try to get rid of them.