I have a problem with a school homework. I have to write a few function, and I have problem with the ShiftPointerVal which argument is a pointer to int. This is the most important in that homework, I mean that argument. I have that issue: error: lvalue required as left operand of assignment. Target of this function is to make a pointer bigger by one. I don't know how to write this function so the pointer to int can be an argument.
void ShiftPointerVal (int& pointer)
{
&pointer = &pointer + 1;
}
void ShiftPointerRef (int *&pointer)
{
pointer = pointer + 1;
}
void ShiftPointerPointer (int **pointer)
{
*pointer = *pointer +1;
}
int main ()
{
int number = 5;
int* p_number = &number;
std::cout << "\n----- 4 -----\n";
std::cout << &number << "\t" << p_number << "\n";
std::cout << *p_number << "\n";
ShiftPointerVal (p_number);
std::cout << &number << "\t" << p_number << "\n";
std::cout << *p_number << "\n";
std::cout << "\n----- 5 -----\n";
std::cout << &number << "\t" << p_number << "\n";
std::cout << *p_number << "\n";
ShiftPointerRef (*&p_number);
std::cout << &number << "\t" << p_number << "\n";
std::cout << *p_number << "\n";
std::cout << "\n----- 6 -----\n";
std::cout << &number << "\t" << p_number << "\n";
std::cout << *p_number << "\n";
ShiftPointerPointer (&p_number);
std::cout << &number << "\t" << p_number << "\n";
std::cout << *p_number << "\n";
}
Related
I have following test example:
#include <iostream>
#include <vector>
void foo (std::vector<int> value) {
std::cout << "value "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
void foo2 (std::vector<int>&& rvalure_ref) {
std::cout << "rvalue_ref "
<< &rvalure_ref
<< " "
<< rvalure_ref.data()
<< " "
<< rvalure_ref.size()
<< std::endl;
}
int main() {
std::vector<int> value(5, 0);
std::cout << "init "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
foo(std::move(value));
std::cout << "done "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
The result of the code above is:
init 0x7ffed27c6450 0x56480bc1eeb0 5
value 0x7ffed27c6470 0x56480bc1eeb0 5
done 0x7ffed27c6450 0 0
Looks great:
Now, move to:
#include <iostream>
#include <vector>
void foo (std::vector<int> value) {
std::cout << "value "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
void foo2 (std::vector<int>&& rvalure_ref) {
std::cout << "rvalue_ref "
<< &rvalure_ref
<< " "
<< rvalure_ref.data()
<< " "
<< rvalure_ref.size()
<< std::endl;
}
int main() {
std::vector<int> value(5, 0);
std::cout << "init "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
foo2(std::move(value));
std::cout << "done "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
The result is:
init 0x7ffccc93a5c0 0x56124b3a8eb0 5
rvalue_ref 0x7ffccc93a5c0 0x56124b3a8eb0 5
done 0x7ffccc93a5c0 0x56124b3a8eb0 5
My problem is:
For the 1st case, it is perfectly called by "move semantics", and as you see, the ownership of the vector has been transfered to the function parameter. Finally, at "done", the data is null to verify the the vector at main() no longer owns the vector.
Now to explicitly claim the parameter is "rvalue reference", as case 2. As you see, actually it is like "call by (l)reference".
How can I figure out it?
#include <iostream>
using namespace std;
void swap(int& a, int& b)
{
cout << "address of a: " << &a << " value of a: " << a << endl;
cout << "address of b: " << &b << " value of b: " << b << endl;
int tmp{move(a)};
cout << "address of tmp: " << &tmp << " value of tmp: " << tmp << endl;
a = move(b);
b = move(tmp);
cout << "address of a: " << &a << " value of a: " << a << endl;
cout << "address of b: " << &b << " value of b: " << b << endl;
}
void swap_no_move(int& a, int& b)
{
cout << "address of a: " << &a << " value of a: " << a << endl;
cout << "address of b: " << &b << " value of b: " << b << endl;
int tmp{ a };
cout << "address of tmp: " << &tmp << " value of tmp: " << tmp << endl;
a = b;
b = tmp;
cout << "address of a: " << &a << " value of a: " << a << endl;
cout << "address of b: " << &b << " value of b: " << b << endl;
}
int main() {
int a = 10;
int b = 5;
swap(a, b);
cout << endl;
int c = 10;
int d = 5;
swap_no_move(c, d);
cin.get();
return 0;
}
I have two swap functions: swap and swap_no_move. According to what I read from the book, there should be no "copy" in function swap which means the address of tmp should be the same for tmp and an in function swap. However, the output I got shows there is no difference between these two functions, did I do something wrong?
The definition
int tmp{move(a)};
doesn't move the reference or the variable a itself. It creates a brand new variable tmp which the compiler allocates space for. Then the value of a is moved into tmp.
And since moving int values can't really be done, it's exactly the same as
int tmp = a;
Error in terminal of VS Code:
/tmp/ccU4qzPn.o: In function main':
azul.cpp:(.text+0xa7e): undefined reference toMosaic::Mosaic()'
azul.cpp:(.text+0xa86): undefined reference to `Mosaic::printMosaic()'
collect2: error: ld returned 1 exit status*
azul.cpp
#include "Mosaic.h"
#include <iostream>
// int menuOption;
std::string playerOne;
std::string playerTwo;
void printMenu() {
std::cout << " " << std::endl;
std::cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << std::endl;
std::cout << "| |" << std::endl;
std::cout << "| Welcome to Azul |" << std::endl;
std::cout << "| |" << std::endl;
std::cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << std::endl;
std::cout << "| ----------- |" << std::endl;
std::cout << "| Main Menu |" << std::endl;
std::cout << "| ----------- |" << std::endl;
std::cout << "| 1. New Game |" << std::endl;
std::cout << "| 2. Load Game |" << std::endl;
std::cout << "| 3. Credits |" << std::endl;
std::cout << "| 4. Help |" << std::endl;
std::cout << "| 5. Quit |" << std::endl;
std::cout << "| |" << std::endl;
std::cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << std::endl;
std::cout << " " << std::endl;
}
int main(void) {
printMenu();
std::cout << "Enter Your Choice: " << std::endl;
char menuOption;
std::cin >> menuOption;
if (menuOption == '1') {
std::cout << "\n\nStarting a new game..." << std::endl;
std::cout << "\nEnter Name for Player One:" << std::endl;
std::cin >> playerOne;
std::cout << "\nEnter Name for Player Two:" << std::endl;
std::cin >> playerTwo;
std::cout << "\n\nLet's play!\n" << std::endl;
std::cout << "-----------" << std::endl;
std::cout << "Start Round" << std::endl;
std::cout << "-----------" << std::endl;
std::cout << "\nMosaic for " << playerOne << ":" << std::endl;
Mosaic* mosaic = new Mosaic();
mosaic->printMosaic();
}
return EXIT_SUCCESS;
};
Mosaic.cpp
#include "Mosaic.h"
#include <iostream>
Mosaic::Mosaic(){
}
Mosaic::~Mosaic(){
}
void Mosaic::printMosaic() {
for (int i = 1; i < 6; i++)
{
std::cout << i << ". ";
for (int j = 6; j > i; j--)
{
std::cout << " ";
}
for (int j = 0; j < i; j++)
{
std::cout << ".";
}
std::cout << "|| . . . . ." << std::endl;
}
}
Mosaic.h
class Mosaic
{
public:
Mosaic();
~Mosaic();
void printMosaic();
};
I think the problem is at the pointer
Mosaic* mosaic = new Mosaic();
I try to make uncrustify to go from:
std::cout << "Rho P " << this->myThermo->getConst("rhoSolide") << "\n";
std::cout << "MDB = " << mdb << "\n";
std::cout << "MDC = " << mdc << "\n";
std::cout << "T = " << T << "\n";
std::cout << "P = " << P << "\n";
std::cout << "Surface = " << S << "\n";
to :
std::cout << "Rho P " << this->myThermo->getConst("rhoSolide") << "\n";
std::cout << "MDB = " << mdb << "\n";
std::cout << "MDC = " << mdc << "\n";
std::cout << "T = " << T << "\n";
std::cout << "P = " << P << "\n";
std::cout << "Surface = " << S << "\n";
But so far I failed miserably! Is this even possible, and if so, can I get any hints?
No, there is no such angle align option in Uncrustify as of now.
Only
# Whether to align lines that start with '<<' with previous '<<'.
#
# Default: true
align_left_shift = false # true/false
which for some reason is defaulted be always used
I am trying to get the valid memory address of CHAR A4 and b5 but when I try to reach that address using Hex Editor it's not reading the address that I have already got in my console output after compiling.
Hex Editor is validating the address as invalid address.
My Code:
#include <iostream>
using namespace std;
main ()
{
{ //INT
cout << "INT" << '\n';
int a = 2, b = 3;
cout << "Result: " << "for " << "int a " << "= " << a << '\n';
cout << "Result: " << "for " << "int a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "int b " << "= " << b << '\n';
cout << "Result: " << "for " << "int b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//SHORT
cout << "SHORT" << '\n';
short a = 2, b = 3;
cout << "Result: " << "for " << "short a " << "= " << a << '\n';
cout << "Result: " << "for " << "short a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "short b " << "= " << b << '\n';
cout << "Result: " << "for " << "short b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//FLOAT
cout << "FLOAT" << '\n';
float a = 2, b = 3.1;
cout << "Result: " << "for " << "float a " << "= " << a << '\n';
cout << "Result: " << "for " << "float a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "float b " << "= " << b << '\n';
cout << "Result: " << "for " << "float b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//DOUBLE
cout << "DOUBLE" << '\n';
double a = 20, b = 30.1;
cout << "Result: " << "for " << "double a " << "= " << a << '\n';
cout << "Result: " << "for " << "double a " << "= " << a << " " << "at " << "address " << &a << '\n';
cout << "Result: " << "for " << "double b " << "= " << b << '\n';
cout << "Result: " << "for " << "double b " << "= " << b << " " << "at " << "address " << &b << '\n';
cout << "-----------------------------------------" << '\n';
}
{
//CHAR
cout << "CHAR" << '\n';
char A4 = 'A' , b5 = 'B' ;
cout << "Result: " << "for " << "Char A4 " << "= " << A4 << '\n';
cout << "Result: " << "for " << "Char A4 " << "= " << A4 << " " << "at " << "address " << &A4 << '\n';
cout << "Result: " << "for " << "Char b5 " << "= " << b5 << '\n';
cout << "Result: " << "for " << "Char b5 " << "= " << b5 << " " << "at " << "address " << &b5 << '\n';
cout << "-----------------------------------------" << '\n';
}
}
Check out the list of overloads for operator<< for streams. The one for char const* assumes a zero-terminated string at that address. What you want is the overload for void const*. For other types of pointees, that conversion is done implicitly by the compiler, for char you need to make it explicitly yourself:
cout << static_cast<void const*>(&b5) << endl;
char A4 = 'A';
cout << &A4;
Is printing char*, when you print a char* the standard library tries to print a null terminated character string. As you only have a single character there is no null terminator so junk gets printed until the standard library happens to find a null byte, this is undefined behaviour.
To print a pointer rather than a string you need to cast to a different pointer type, for example:
char A4 = 'A';
cout << static_cast<void*>(&A4);