Why do character pointers have nonsense? [duplicate] - c++

This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 1 year ago.
int main() {
int x = 69;
int* px = &x;
cout << px << "\n" << *px << "\n\n";
float z = 69.69;
float* pz = &z;
cout << pz << "\n" << *pz << "\n\n";
char y = 'y';
char* pc = &y;
cout << pc << "\n" << *pc;
return 0;
}
This outputs:
004FFD28
69
004FFD10
69.69
y╠╠╠╠╠╠╠╠²O
y
Why does the char pointer have some weird value that's not a number?

Outputting a char * value is interpreted as outputting a C-style 0-terminated string. Which your pointer does not point to, so you get garbage.

Related

How a const pointer has a different value than a const variable it points to when i try to modify using an another pointer and const_cast? [duplicate]

This question already has answers here:
Two different values at the same memory address
(7 answers)
changing the value of const variable in C++ [duplicate]
(8 answers)
Closed 22 days ago.
This post was edited and submitted for review 22 days ago.
could anyone explain me the last five lines of this code? How do the *ptr and x have different values even though the *ptr is pointing to x?
What actually happens when i use the const_cast. All i know is it removes the constness nature. what does const_cast returns?
int main()
{
const int x = 5;
int y = 10;
// int* ptr1 = &x; //A value of type "const int * " can't be used to initialize an
entity of type" int * "
const int* ptr1 = &x;
const int* ptr2 = &y;
const int* ptr = ptr1;
int* ptr3 = const_cast<int*>(ptr1);
int* ptr4 = const_cast<int*>(ptr2);
//*ptr1 = 50; //expression must be a modifiable lvalue
//*ptr2 = 40; //expression must be a modifiable lvalue
*ptr4 = 40;
cout << y << endl;
*ptr3 = 50;
cout << *ptr1 << endl;//atleast ptr1 has to print the x value
cout << *ptr3 << endl;
cout << *ptr << endl;
cout << x << endl;
}
The output of these lines of the main function:
*ptr3 = 50;
cout << *ptr1 << endl;// 50
cout << *ptr3 << endl;//50
cout << *ptr << endl;//50
cout << x << endl;//5
How declaring a variable as const differs from declaring a pointer as const int* that actually points to non-constant variable?
And I'm not clear in the difference between these below codes:
1)
const int x = 5;
const int* ptr1 = &x;
2)
int y = 10;
const int* ptr2 = &y; //does this actually make the variable
it points to as constant? can't we
modify the value of this variable here
after using any other variables or pointers?
Forgive if i made any irrelevant questions. Thanks in advance.

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

Changing pointers in functions [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 4 years ago.
I am new to c++ coding and this question may seem childish to you all. But I am really not able to come up on a solution to my problem. Please help.
#include <iostream>
using namespace std;
int* getnew(int* x){
int temp = *x;
int* y = &temp;
cout << "from function address of y = " << y
<< " and value of y = " << *y << endl;
return y;
}
int main(){
int x = 100;
int* y = getnew(&x);
cout << "address of y = " << y;
cout << " and value of y = " << *y << endl;
return 0;
}
Output of this code :
from function address of y = 0x7ffee52aa914 and value of y = 100
address of y = 0x7ffee52aa914 and value of y = 1
What I thought should be the output :
from function address of y = 0x7ffee52aa914 and value of y = 100
address of y = 0x7ffee52aa914 and value of y = 100
Can someone explain where am I going wrong ?
Since you’re getting the address of temp, wich was declared inside a function, it’s scope belongs only to the function.
So, when you leave get_new() the temp var no longe exists ( get deallocated ). Then when you acess the same address ( the temp address) in main you access a space of memory that isn’t reserved, wich means that that space can be freely used by other vars and stuff of the program.
Try to use malloc to allocate a new space of memory and guarantee that the var still lives after the get_new returns (finishes). Here’s a example:
#include <iostream>
using namespace std;
int* getnew(int* x){
int temp = *x;
// return a memory address pointing to a ‘ sizeof(int )’ bytes reserved space.
int *y = malloc( sizeof(int) );
cout << "from function address of y = " << y << " and value of y = " << *y << endl;
return y;
}
int main(){
int x = 100;
int* y = getnew(&x);
cout << "address of y = " << y;
cout << " and value of y = " << *y << endl;
return 0;
}
EDIT:
Also do not forget to deallocate the memory when you're finished using it ( no longer needs it):
#include <iostream>
using namespace std;
int* getnew(int* x){
int temp = *x;
// return a memory address pointing to a ‘ sizeof(int )’ bytes reserved space.
int *y = malloc( sizeof(int) );
cout << "from function address of y = " << y << " and value of y = " << *y << endl;
return y;
}
int main(){
int x = 100;
int* y = getnew(&x);
cout << "address of y = " << y;
cout << " and value of y = " << *y << endl;
/*********** same code until here********/
//use free for each memory allocated by malloc()
free(y); //deallocates the memory pointed by *y*
free(other_y_if_it_exists);
return 0;
}

Reassigning C++ reference variables [duplicate]

This question already has answers here:
Can we reassign the reference in C++?
(7 answers)
Closed 7 years ago.
I am trying to understand C++ reference variables. This link seems to indicate that a pointer can be reassigned while a reference should be assigned at initialization. difference between pointer and reference. I have the following code below. I have run it on a debian system. The output is also shown below. The output seems to indicate that reference can be reassigned as well.It would be great if someone could clarify.
#include <iostream>
using namespace std;
int main()
{
int x = 5;
int y = 6;
int *p;
p = &x;
cout << "content of p " << *p << endl;
p = &y;
cout << "content of p " << *p << endl;
*p = 10;
cout << "content of p " << *p << endl;
/*A reference must be assigned at initialization*/
int &r = x;
cout << "content of r " << r << endl;
r = y;
cout << "content of r " << r << endl;
return 0;
}
Output
content of p 5
content of p 6
content of p 10
content of r 5
content of r 10
What you see here is the value being assigned to the variable referenced by the referencing variable.
In other words:
You did not assign new value to the referencing variable. You assigned a new value to the referenced variable.

On Pointers and Dereferencing

I've been working on learning C++ lately and picked up the book "C++ Through Game Programming". I'm on the chapter on Pointers and I've been presented an example that I have a question about. The code is this:
#include "stdafx.h"
#include <iostream>
using namespace std;
void badSwap(int x, int y);
void goodSwap(int* const pX, int* const pY);
int main()
{
int myScore = 150;
int yourScore = 1000;
cout << "Original values\n";
cout << "myScore: " << myScore << "\n";
cout << "yourScore: " << yourScore << "\n\n";
cout << "Calling badSwap()\n";
badSwap(myScore, yourScore);
cout << "myScore: " << myScore << "\n";
cout << "yourScore: " << yourScore << "\n\n";
cout << "Calling goodSwap()\n";
goodSwap(&myScore, &yourScore);
cout << "myScore: " << myScore << "\n";
cout << "yourScore: " << yourScore << "\n";
cin >> myScore;
return 0;
}
void badSwap(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
void goodSwap(int* const pX, int* const pY)
{
//store value pointed to by pX in temp
int temp = *pX;
//store value pointed to by pY in address pointed to by pX
*pX = *pY;
//store value originally pointed to by pX in address pointed to by pY
*pY = temp;
}
In the goodSwap() function there's the line:
*pX = *pY;
Why would you dereference both sides of the assignment? Isn't that the equivalent of saying "1000 = 150"?
Why would you dereference both sides of the assignment? Isn't that the equivalent of saying "1000 = 150"?
No, just like the following:
int x = 1000;
int y = 150;
x = y;
is not the equivalent of saying "1000 = 150". You're assigning to the object, not to the value it presently contains.
The below is precisely the same (since the expression *px is an lvalue referring to the object x, and the expression *py is an lvalue referring to the object y; they're literally aliases, not some strange, disconnected version of the objects' numerical values):
int x = 1000;
int y = 150;
int* px = &x;
int* py = &y;
*px = *py;
*px=*py means we are assigning the value not address, from address of py to address of px.
Skip the chapter of the book or buy another one:
there is no need of using plain pointers in C++ nowadays.
Also there is a std::swap function, that does the things C++isch.