Incrementing a *int
If i pass in a pointer to an integer and i want to increment the int i was under the impression that i simply dereference the pointer like so :
void myclass::myfunc(int *i)
{
*i++;
}
int p=0;
myfunc(&p);
when i do this and debug the actual address of p is being changed rather than the number. What am i doing wrong? (p is actually a member variable of a class but the function operating on the address of p is also a member function of the same class)
It's to do with precedence, if you write:
it increments the pointer,
You should change it to this:
[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]
[edited by - Lektrix on May 26, 2003 10:08:50 AM]
*i++
it increments the pointer,
i
and then dereferences it ( *(i++)
), i.e. the function does nothing that changes the value pointed to by the pointer.You should change it to this:
void myFunction(int *i)
{
(*i)++; // or ++(*i);
}
int main()
{
int p = 0;
myFunction(&p);
std::cout << p << std::endl; // prints "1"
return 0;
}
[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]
[edited by - Lektrix on May 26, 2003 10:08:50 AM]
what you want to do is
(*i)++
what c++ implicitly does is
*(i++)
where only due to the nature of the postincrement, which binds stronger than the *, the increment is done after returning the value.
This due to operator precedence.
---------------------------
I may be getting older, but I refuse to grow up
(*i)++
what c++ implicitly does is
*(i++)
where only due to the nature of the postincrement, which binds stronger than the *, the increment is done after returning the value.
This due to operator precedence.
---------------------------
I may be getting older, but I refuse to grow up
you should instead just pass it like such
int p=0;
your_function(p);
class::your_function(int &p)
{
p++;
}
int p=0;
your_function(p);
class::your_function(int &p)
{
p++;
}
quote:Original post by dopeflow
you should instead just pass it like such
int p=0;
your_function(p);
class::your_function(int &p)
{
p++;
}
It''s mainly an issue of style, but I disagree. Passing a pointer makes it obvious that the function is going to change the value of the argument, whereas if you pass a reference, it looks the same as passing by value, making it less obvious that the value could be changed.
Besides, passing by reference doesn''t work in pure C...
quote:Original post by sbennett
Besides, passing by reference doesn't work in pure C...
Well that's irrelevant if you're programming in C++.
quote:Original post by sbennett
Passing a pointer makes it obvious that the function is going to change the value of the argument, whereas if you pass a reference, it looks the same as passing by value, making it less obvious that the value could be changed.
Hmmm, in my opinion it just makes for more work to pass by pointer when you can pass by reference, but I can see where you are coming from.
[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]
[edited by - Lektrix on May 26, 2003 10:30:13 AM]
quote:Original post by sbennett
It''s mainly an issue of style, but I disagree. Passing a pointer makes it obvious that the function is going to change the value of the argument, whereas if you pass a reference, it looks the same as passing by value, making it less obvious that the value could be changed.
In C++ you indicate non-changing parameters by using the
const
keyword: void f(int &p){ // change value of p}// rather a made-up example, since you wouldn''t normally// want to pass primitive parameters by referencevoid f(const int &p){ // don''t change value of p}
So in C++ it''s obvious, too
darookie, think of the call:
f(a);
f(&a);
f(a) maybe pass by reference, pass by const reference or pass by value. You''ll never know. f(&a) clearly gives the pointer so it can be assumed that ''a'' will be changed. Of course nobody would name their functions ''f'' so in reality it''s usually a bit easier to tell what will happen to the parameters..
f(a);
f(&a);
f(a) maybe pass by reference, pass by const reference or pass by value. You''ll never know. f(&a) clearly gives the pointer so it can be assumed that ''a'' will be changed. Of course nobody would name their functions ''f'' so in reality it''s usually a bit easier to tell what will happen to the parameters..
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement