i have a code:
#include <iostream>
using namespace std;
int main()
{
int *a, y = 6 , *yPtr = &y;
cout << "y:" << y << "| &y:" << &y << "| yptr:" << yPtr << "| *yptr:" << *yPtr << " | &yptr:" << &yPtr << " |a:" << a << endl;
*a = y;
cout<< "a:"<<a<<endl;
return 0;
}
when i assign *a to y *a = y then *a value not printed for me
This is because you never initialize a itself. *a points to who-knows-where, some random location. So you set some random location to 6.
As it's probably pointed outside of legal space, your program is probably quitting before it gets to the cout statement.
Related
Considere the following code in c++:
#include <iostream>
using namespace std;
int main() {
int x=2, y;
int *p = &x;
int **q = &p;
std::cout << p << std::endl;
std::cout << q << std::endl;
std::cout << *p << std::endl;
std::cout << x << std::endl;
std::cout << *q << std::endl;
*p = 8;
*q = &y;
std::cout << "--------------" << std::endl;
std::cout << p << std::endl;
std::cout << q << std::endl;
std::cout << *p << std::endl;
std::cout << x << std::endl;
std::cout << *q << std::endl;
return 0;
}
The output of code is (of course the list numbers is not the part of output):
0x7fff568e52e0
0x7fff568e52e8
2
2
0x7fff568e52e0
'-----------'
0x7fff568e52e4
0x7fff568e52e8
0
8
0x7fff568e52e4
Except for 7 and 9, all outputs were expected for me. I appreciate someone explaining them to me.
The variable y was not initialized
int x=2, y;
So it has an indeterminate value.
As the pointer q points to the pointer p
int **q = &p;
then dereferencing the pointer q you get a reference to the pointer p.
So this assignment statement
*q = &y;
in fact is equivalent to
p = &y;
That is after the assignment the pointer p contains the address of the variable y.
So this call
std::cout << p << std::endl;
now outputs the address of the variable y.
0x7fff568e52e4
and this call
std::cout << *p << std::endl;
outputs the indeterminate value of y that happily is equal to 0.
9. 0
This is what i have:
void g(int *&x) {
int a = 3;
x = &a;
}
void h(const int *&x) {
int b = 2;
x = &b;
}
int main() {
int *p = new int;
*p = 5;
g(p);
cout << p << " " << *p << endl; // Print #2
cout << p << " " << *p << endl; // Print #3
const int*p1 = p;
h(p1);
cout << p << " " << *p << endl; // Print #4
cout << p << " " << *p << endl; // Print #5
}
From what i understand, Print#2 and Print#3 should have the same result but it isn't when i compile it. It goes for Print#4andPrint#5 too. Can someone help me?
Updated : This is the output looks like when i compiled it on my computer:
00EFF9D4 3 //1
00EFF9D4 1552276352 //2
00EFF9D4 2 //3
00EFF9D4 1552276352 //4
shouldn't (1) and (2) be the same ? (3) and (4) either.
I assume you mean int a in g().
Your function make the pointer point to a local variable, which after the termination of the function, will go out of scope.
You then dereference the pointer, which invokes Undefined Behavior.
I'm trying to make something in C++ and I have a problem. I have this code:
#include <iostream>
#include <string>
//---MAIN---
using namespace std;
int af1 = 1;
int af2 = 1;
void lettersort(int cnt1) {
cout << "RESULT:" << cnt1 << endl;
cnt1++;
cout << "RESULT WHEN+:" << cnt1 << endl;
cout << "RESULT IN GLOBAL INT:" << af2 << endl;
}
int main()
{
lettersort(af2);
return 0;
}
So is there any way so that cnt1++ affects af2 too, to make it bigger ? I don't want to use af2++ directly because I want to sometimes use af1.
At the moment you are just passing af2 to cnt1 by value, so any changes to cnt1 are strictly local to the function lettersort. In order to get the behaviour you want you need to pass your cnt1 parameter by reference. Change:
void lettersort(int cnt1)
to:
void lettersort(int &cnt1)
You are passing the argument by value. I.e., you are copying the value of af1 to a local variable in lettersort. This integer is then incremented, and disposed of when the function ends, without affecting the original af1. If you want the function to be able to affect af1, you should pass the argument by reference:
void lettersort(int& cnt1) { // Note the "&"
if i understood your question:
there are 2 ways you can do that.
make lettersort function return the new value, and put it in af2
int lettersort(int cnt1) {
cout << "RESULT:" << cnt1 << endl;
cnt1++;
cout << "RESULT WHEN+:" << cnt1 << endl;
cout << "RESULT IN GLOBAL INT:" << af2 << endl;
return cnt1;
}
int main()
{
af2 = lettersort(af2);
return 0;
}
pass the value by reference. you can read about it here, but generally its about passing a pointer to that value. meaning whatever you do on the argument you are passing, will happen on the original var.
example:
void foo(int &y) // y is now a reference
{
using namespace std;
cout << "y = " << y << endl;
y = 6;
cout << "y = " << y << endl;
} // y is destroyed here
int main()
{
int x = 5;
cout << "x = " << x << endl;
foo(x);
cout << "x = " << x << endl;
return 0;
}
here you have to just modified the argument pass to lettersort
function as passed by reference.
for example if you declare and initialize any variable like:
int a=10; int &b = a;
now a and b refer to the same value.if you change a then the changes
also reflect in b also.
so,
cout << a; cout << b;
both statement produce the same result across the program. so using
this concept i modified the function argument and made it as by
reference.
your correct code is :
#include <iostream>
#include <string>
using namespace std;
int af1 = 1;
int af2 = 1;
void lettersort(int &cnt1) {
cout << "RESULT:" << cnt1 << endl;
cnt1++;
cout << "RESULT WHEN+:" << cnt1 << endl;
cout << "RESULT IN GLOBAL INT:" << af2 << endl;
}
int main()
{
lettersort(af2);
return 0;
}
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.
I have a program with one structnamed sample, it contains 2 int members and one char *. when creating 2 objects called a and b, I try assign a new dynamic string to a with the pointer and then copy all the values to b. so b = a. But later on when try to make changes to a like this : a.ptr[1] = 'X'; the pointer in b also changes. I want to know why, and how can I solve this.
struct Sample{
int one;
int two;
char* sPtr = nullptr;
};
int _tmain(int argc, _TCHAR* argv[])
{
Sample a;
Sample b;
char *s = "Hello, World";
a.sPtr = new char[strlen(s) + 1];
strcpy_s(a.sPtr, strlen(s) + 1, s);
a.one = 1;
a.two = 2;
b.one = b.two = 9999;
b = a;
cout << "After assigning a to b:" << endl;
cout << "b=(" << b.one << "," << b.two << "," << b.sPtr << ")" << endl << endl;
a.sPtr[1] = 'X' ;
cout << "After changing sPtr[1] with 'x', b also changed value : " << endl;
cout << "a=(" << a.one << "," << a.two << "," << a.sPtr << ")" << endl;
cout << "b=(" << b.one << "," << b.two << "," << b.sPtr << ")" << endl;
cout << endl << "testing adresses for a and b: " << &a.sPtr << " & b is: " << &b.sPtr << endl;
return 0;
}
Your struct contains a char*. When you assign all values in a to b, the pointer is also copied.
This means that a and b now point to the same char array. Therefore changing a value in this char array changes it for both structs.
If you do not want this, make a new char array for b and use strcpy.
You are copying the pointer not the value. To solve this you could override your assignment operator in the structure:
struct Sample{
int one;
int two;
char* sPtr = nullptr;
Sample& operator=(const Sample& inputSample)
{
one = inputSample.one;
two = inputSample.two;
sPtr = new char[strlen(inputSample.sPtr) + 1];
strcpy (sPtr, inputSample.sPtr);
return *this;
}
};