Function does not change passed pointer C++ - c++

I have my function and I am filling targetBubble there, but it is not filled after calling this function, but I know it was filled in this function because I have there output code.
bool clickOnBubble(sf::Vector2i & mousePos, std::vector<Bubble *> bubbles, Bubble * targetBubble) {
targetBubble = bubbles[i];
}
And I am passing the pointer like this
Bubble * targetBubble = NULL;
clickOnBubble(mousePos, bubbles, targetBubble);
Why it is not working?

Because you are passing a copy of pointer. To change the pointer you need something like this:
void foo(int **ptr) //pointer to pointer
{
*ptr = new int[10]; //just for example, use RAII in a real world
}
or
void bar(int *& ptr) //reference to pointer (a bit confusing look)
{
ptr = new int[10];
}

You are passing the pointer by value.
Pass a reference to the pointer if you want it updated.
bool clickOnBubble(sf::Vector2i& mousePos, std::vector<Bubble *> bubbles, Bubble *& t)

if you write
int b = 0;
foo(b);
int foo(int a)
{
a = 1;
}
you do not change 'b' because a is a copy of b
if you want to change b you would need to pass the address of b
int b = 0;
foo(&b);
int foo(int *a)
{
*a = 1;
}
same goes for pointers:
int* b = 0;
foo(b);
int foo(int* a)
{
a = malloc(10); // here you are just changing
// what the copy of b is pointing to,
// not what b is pointing to
}
so to change where b points to pass the address:
int* b = 0;
foo(&b);
int foo(int** a)
{
*a = 1; // here you changing what b is pointing to
}
hth

You cannot change the pointer unless you pass it by (non const) reference or as a double pointer. Passing by value makes a copy of the object and any changes to the object are made to the copy, not the object. You can change the object that the pointer points to, but not the pointer itself if you pass by value.
Have a read of this question to help understand the differences in more detail When to pass by reference and when to pass by pointer in C++?

Related

swap function not works as expectd [duplicate]

I have my function and I am filling targetBubble there, but it is not filled after calling this function, but I know it was filled in this function because I have there output code.
bool clickOnBubble(sf::Vector2i & mousePos, std::vector<Bubble *> bubbles, Bubble * targetBubble) {
targetBubble = bubbles[i];
}
And I am passing the pointer like this
Bubble * targetBubble = NULL;
clickOnBubble(mousePos, bubbles, targetBubble);
Why it is not working?
Because you are passing a copy of pointer. To change the pointer you need something like this:
void foo(int **ptr) //pointer to pointer
{
*ptr = new int[10]; //just for example, use RAII in a real world
}
or
void bar(int *& ptr) //reference to pointer (a bit confusing look)
{
ptr = new int[10];
}
You are passing the pointer by value.
Pass a reference to the pointer if you want it updated.
bool clickOnBubble(sf::Vector2i& mousePos, std::vector<Bubble *> bubbles, Bubble *& t)
if you write
int b = 0;
foo(b);
int foo(int a)
{
a = 1;
}
you do not change 'b' because a is a copy of b
if you want to change b you would need to pass the address of b
int b = 0;
foo(&b);
int foo(int *a)
{
*a = 1;
}
same goes for pointers:
int* b = 0;
foo(b);
int foo(int* a)
{
a = malloc(10); // here you are just changing
// what the copy of b is pointing to,
// not what b is pointing to
}
so to change where b points to pass the address:
int* b = 0;
foo(&b);
int foo(int** a)
{
*a = 1; // here you changing what b is pointing to
}
hth
You cannot change the pointer unless you pass it by (non const) reference or as a double pointer. Passing by value makes a copy of the object and any changes to the object are made to the copy, not the object. You can change the object that the pointer points to, but not the pointer itself if you pass by value.
Have a read of this question to help understand the differences in more detail When to pass by reference and when to pass by pointer in C++?

Why is pointer dropping value in C++? [duplicate]

I have my function and I am filling targetBubble there, but it is not filled after calling this function, but I know it was filled in this function because I have there output code.
bool clickOnBubble(sf::Vector2i & mousePos, std::vector<Bubble *> bubbles, Bubble * targetBubble) {
targetBubble = bubbles[i];
}
And I am passing the pointer like this
Bubble * targetBubble = NULL;
clickOnBubble(mousePos, bubbles, targetBubble);
Why it is not working?
Because you are passing a copy of pointer. To change the pointer you need something like this:
void foo(int **ptr) //pointer to pointer
{
*ptr = new int[10]; //just for example, use RAII in a real world
}
or
void bar(int *& ptr) //reference to pointer (a bit confusing look)
{
ptr = new int[10];
}
You are passing the pointer by value.
Pass a reference to the pointer if you want it updated.
bool clickOnBubble(sf::Vector2i& mousePos, std::vector<Bubble *> bubbles, Bubble *& t)
if you write
int b = 0;
foo(b);
int foo(int a)
{
a = 1;
}
you do not change 'b' because a is a copy of b
if you want to change b you would need to pass the address of b
int b = 0;
foo(&b);
int foo(int *a)
{
*a = 1;
}
same goes for pointers:
int* b = 0;
foo(b);
int foo(int* a)
{
a = malloc(10); // here you are just changing
// what the copy of b is pointing to,
// not what b is pointing to
}
so to change where b points to pass the address:
int* b = 0;
foo(&b);
int foo(int** a)
{
*a = 1; // here you changing what b is pointing to
}
hth
You cannot change the pointer unless you pass it by (non const) reference or as a double pointer. Passing by value makes a copy of the object and any changes to the object are made to the copy, not the object. You can change the object that the pointer points to, but not the pointer itself if you pass by value.
Have a read of this question to help understand the differences in more detail When to pass by reference and when to pass by pointer in C++?

emplace_back with pointer and unique pointers

In relation to someFunctionOne and someFunctionTwo.
Is the end result still the same when each is emplaced into the map ?
Does the pointer get upgraded to a smart pointer ?
struct MyStruct {
int x = 0;
};
std::unordered_map<int, std::unique_ptr<MyStruct>> m_map;
void someFunctionOne(int id, std::unique_ptr<MyStruct>& myStruct){
m_map.emplace(id, std::move(myStruct));
}
void someFunctionTwo(int id, MyStruct * myStruct){
m_map.emplace(id, myStruct);
}
int main()
{
someFunctionOne(452, std::make_unique<MyStruct>());
someFunctionTwo(10, new MyStruct);
m_map.clear();
return 0;
}
Does the pointer get upgraded to a smart pointer ?
The correct term is Implicit type conversion. But yes, that is what would happen in this line:
m_map.emplace(id, myStruct);
This is because unique_pointer has a single value constructor which has the signature:
explicit unique_ptr( pointer p ) noexcept;
This is what would be called to convert the pointer to a unique_ptr.

c++ in function- an error for "a reference .. can not be initialized with a value"

I 'm still confused for my problem after spending an amount of time to digging related posts/online resources.
My sample codes (test.cc) are :
void testsub(const int* &xx );
int main ()
{
int* xx;
xx= new int [10];
testsub(xx);
}
void testsub(const int* & xx){}
The compiling error message(pgcpp) read
"test.cc", line 7: error: a reference of type "const int *&" (not const-qualified)
cannot be initialized with a value of type "int *"
testsub(xx);
^
1 error detected in the compilation of "test.cc"."
Why? Your help is appreciated.
Best wishes,
Ting
int* cannot be used where the argument type is const int* &.
Say you have:
const int a = 10;
void foo(const int* & ip)
{
ip = &a;
}
int main()
{
int* ip = NULL;
foo(ip);
*ip = 20; // If this were allowed, you will be able to
// indirectly modify the value of "a", which
// is not good.
}
As the error message says, the argument type is incompatible; the function wants a pointer to const int, while you supply a pointer to int.
If you're asking why that's incompatible: allowing it would allow you to break const-correctness, as in this example:
void testsub(const int* &xx ) {
static const int x;
xx = &x;
}
int* xx;
testsub(xx); // Shouldn't be allowed, because...
*xx = 666; // BOOM! modifying a constant object.
Maybe try this
void testsub(const int* xx );
int main ()
{
int xx [10];
testsub(xx);
}
void testsub(const int* xx){}
You don't need the &, because you are passing a pointer as argument.
When you forward a "C-Array" (your int[10]), you will have a pointer to the first element of this array in your function.
void testsub(const int* xx );
int main ()
{
int* xx;
xx= new int [10];
testsub(xx);
}
void testsub(const int* xx){}
I think you got confused by your book, because they always write something like "Call by reference". That doesn't mean to pass the parameter as a reference with the &.
Often it is useful to pass also the size of the array to the function ... so it would like:
void testsub(const int* xx, size_t arraySize);
int main ()
{
int* xx;
xx= new int [10];
testsub(xx, 10);
}
void testsub(const int* xx, size_t arraySize){}
Now you can access the array in your function and you have the possibility to check the index, if you want to access the array with an index.
void testsub(int* xx, size_t arraySize)
{
for(size_t i=0; i<arraySize; ++i)
// ^ this way you will never try to access
// memory, which does not belong to the array
// => no seg fault, or whatever happens
{
// do sth. with the array ... for example setting values to 0
xx[i] = 0;
}
}

Overwriting function parameter in a recursive function?

So I have a class that has a protected pointer member of the following type
int *assigntoThis; // In the constructor I have initialized this to NULL.
I also have a public recursive member function of the same class with the following declaration
bool find(int* parent, std::string nameofnode, int* storeParentinThis);
The recursive function checks through child nodes and if the name of the child Node matches the string passed in as a parameter it will assign the address of parent to storeParentinThis.
This is how I call the function from another function of the same class.
bool find(root, "Thread", assigntoThis);
However, during runtime when I output the value stored in assigntoThis I get 00000000 = NULL. How do I change the value of assigntoThis inside my recursive function?
change to :
bool find(int* parent, std::string nameofnode, int*& storeParentinThis);
explanation:
here is a simplified version of your original code:
foo (int* p) { /// p bahaves as a local variable inside foo
p = 1;
}
int* p = 0;
foo(p);
// here p is still equal 0
this is actually similar to following code:
foo (int i) {
i = 1;
}
int i = 0;
foo(i);
// here i is still equal 0
which I think is easier to understand.
So if we want to return something from a function we must make a pointer to it or a reference to it, going backwards with examples:
foo (int* i) { // pointer example
*i = 1;
}
int i = 0;
foo(&i);
// here i is equal to 1
foo (int& i) { // using reference example
i = 1;
}
int i = 0;
foo(i);
// here i is equal to 1
Now it is easy to apply it to your case:
// pointer example
bool find(int* parent, std::string nameofnode, int** storeParentinThis) {
*storeParentinThis = parent;
}
// reference example
bool find(int* parent, std::string nameofnode, int*& storeParentinThis) {
storeParentinThis = parent;
}