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

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) {}

Related

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

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

why cannot output the address of return directly in c++ by using `cout`?

I am new to c++, and there is a problem that I find cannot directly output the address of return.
when I run test03() it occurs a error like below. But test04() works fine.
C102:"&"要求左值
in English it should mean
C102:& needs a left value
I have put script below, I think the return will be free if it not be used. But hope for a concrete answer.
int change(int a) {
return --a;
}
void test03() {
int a = 10;
cout << "the first address is " << &a << "the later adress is " << &(change(a)) << endl;
}
void test04() {
int a = 10;
int b = change(a);
cout << "the first address is " << &a << "the later adress is " << &b << endl;
}
int main(){
test04();
return 0;
}

Unexpected result for "&array" [duplicate]

This question already has answers here:
Why are `&array` and `array` pointing to the same address?
(2 answers)
How come an array's address is equal to its value in C?
(6 answers)
Closed 1 year ago.
I am confused about array usage as pointer and result of that. Let me explain. When I try this
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int *myPointer = &a;
cout << "*myPointer: \t" << *myPointer << endl;
cout << "myPointer: \t" << myPointer << endl;
cout << "&myPointer: \t" << &myPointer << endl;
cout << "myPointer[0]: \t" << myPointer[0] << endl;
cout << endl;
int myArray[3] = {1,2,3};
cout << "*myArray: \t" << *myArray << endl;
cout << "myArray: \t" << myArray << endl;
cout << "&myArray: \t" << &myArray << endl;
return 0;
}
All of output are exactly what I expected, except last one (&myArray). Lets say my ram something like this:
Address
Variable
Value
0xA100
a
10
0xA101
myPtr
0xA100
0xA102
1
0xA103
2
0xA104
3
0xA105
myArray
0xA102
If I imagine it correctly, then how "&myArray" can be same with "myArray"? Actually, I think it can be anything but 0xA102. Because, this result means that "myArray" is in 0xA102 and value of pointed by myArray is in 0xA102 too. And I know it is weird but it means also, as a value "1", is in 0xA102 too. (At least I got it from this result).
So, I cannot catch the point. I think the result has to be 0xA105 if "&" means address. If yes, why result is not 0xA105. If not, why they have different usage although arrays are pointers.
Is there anybody can clarify the matter?
Thanks.

passing an 2d array pointer to a function in c++ [duplicate]

This question already has answers here:
Passing a 2D array to a C++ function
(18 answers)
Closed 4 years ago.
I know how to pass 1D array pointer to a function by the following code
void fiddleWithArray(int*);
int main(){
int list[10] = {1, 3, 5, 7, 9, 11, 13, 17};
cout << "List at 0 before being passed is... " << list[0][0] << endl;
cout << "List at 1 before being passed is... " << list[1][0] << endl;
fiddleWithArray(list);
cout << "List at 0 after being passed is... " << list[0][0] << endl;
cout << "List at 1 after being passed is... " << list[1][0] << endl;
}
void fiddleWithArray(int* input){
input[0] = 45;
input[1] = 18;
}
However, when I try to do something similar for a 2D array(as shown below) I get an error.
void fiddleWithArray (int** input);
int main ()
{
int list [10][2]={{1,3},{5,7},{9,11},{13,17},{7,4},{5,90},{9,1},{3,25}};
int ** pointer;
pointer=&list;
cout<< "List at 0 before being passed is ... "<< list[0][0]<< endl;
cout<< "List at 1 before being passed is ... "<< list[1][0]<< endl;
fiddleWithArray(pointer);
cout<< "List at 0 after being passed is ... "<< list[0][0]<< endl;
cout<< "List at 1 after being passed is ... "<< list[1][0]<< endl;
}
void fiddleWithArray(int** input)
{
cout << input [6][1]<< endl;
}
The compiler gives an error saying "error: cannot convert ‘int (*)[10][2]’ to ‘int**’ in assignment
pointer=&list;"
I am also open to alternate methods of passing a 2D array pointer to a function.
Keeping your data structure, if you want to pass list to fiddleWithArray you can declare it as
void fiddleWithArray (int input[][2]);
and then, in the main program, call it as
fiddleWithArray(list);
There is also another problem in your program: cout << list[0] does not work. If you want to print the contents of the array when the first index is fixed to 0 you would write something like
cout << list[0][0] << " " << list[0][1]
If you instead intended to write the array where the second index is fixed to 0 or 1, then, to keep things easily, you need a short loop like
for (unsigned int i = 0; i < 10; i++)
cout << list[i][0] << " ";
cout << endl;
Finally, instead of using int[][] you may want to use std::arrayintroduced in C++11.
void fiddleWithArray(int input[10][2])
{
cout << input[6][1] << endl;
}
int main()
{
int list[10][2] = {{1,3},{5,7},{9,11},{13,17},{7,4},{5,90},{9,1},{3,25}};
int (*pointer)[10][2];
pointer=&list;
cout << "List at 0 before being passed is ... "<< list[0][0]<< endl;
cout << "List at 1 before being passed is ... "<< list[1][0]<< endl;
fiddleWithArray(*pointer);
cout << "List at 0 after being passed is ... "<< list[0][0]<< endl;
cout << "List at 1 after being passed is ... "<< list[1][0]<< endl;
}
Will be better using std::array

strange behaviour using sizeof in C++ [duplicate]

This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
When a function has a specific-size array parameter, why is it replaced with a pointer?
(3 answers)
Closed 4 years ago.
I'm very new to C++. From examples, I find this use of sizeof in order to retrieve the length of an array
int main()
{
int newdens[10];
// the first line returns 10 which is correct
std::cout << "\nLength of array = " << (sizeof(v1)/sizeof(*v1)) << std::endl;
std::cout << "\nLength of array = " << (sizeof(v1) << std::endl; //returns 40
std::cout << "\nLength of array = " << (sizeof(*v1)) << std::endl; //returns 4
}
But if i wrote a function like this
#include <iostream>
void myCounter(int v1[])
{
int L, L2, L3;
L = (sizeof(v1)/sizeof(*v1));
L2 = (sizeof(v1));
L3 = (sizeof(*v1));
std::cout << "\nLength of array = " << L << std::endl;
std::cout << "\nLength of array = " << L2 << std::endl;
std::cout << "\nLength of array = " << L3 << std::endl;
}
int main()
{
int v1[10];
std::cout << "\nLength of array = " << (sizeof(v1)/sizeof(*v1)) << std::endl;
std::cout << "\nLength of array = " << (sizeof(v1)) << std::endl;
std::cout << "\nLength of array = " << (sizeof(*v1)) << std::endl;
myCounter(v1);
}
the outputs are L=2, L2 = 8, L3 = 4. I can't understand where the problem is.
How to retrieve the correct lenght of v1 inside the function?
Your problem here is that sizeof() is resolved at compile time. As it has no information about how large is your array, it cannot tell its size. It interprets it as a pointer to an int, which is 64-bit on your machine.
The best way for you is to use std::vector instead of C-style array and use its method size().