+ 2

Question about pointers (more may very well follow ;-|)

I am reading a textbook about c++. The author describes how we declare and define a pointer *px to x, then he says we call function f like this: f(&x). Why is the adress handed over directly when we went out of our way to create a pointer? Why don't we pass the pointer we made? Or why don't we just always write the adress and not initialize pointers at all?

4th Sep 2018, 2:11 PM
HonFu
HonFu - avatar
4 Respuestas
+ 3
HonFu it's like you call a function summation(1,2) directly rather than doing as below : int a = 1; int b = 2; summation(a,b); to conclude, you can define pointer as address of variable and use pointer or direct address as argument to function... I hope I made it clear.. if you have query, feel free to ask
4th Sep 2018, 3:05 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
yup
4th Sep 2018, 4:47 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Ah, I think, I got you! So I can store an address in a pointer. I can do it (for whatever reason) in my main program, but I am also doing it when I'm cbr-ing a function. The pointer-vessel is waiting there in the shape of *px, and I fill this vessel with the call f(&x); I could also choose to pass px, if I had that pointer defined in my main, but that would basically be the same thing as handing over &x.
4th Sep 2018, 4:46 PM
HonFu
HonFu - avatar
0
Ha - perfect. :) Thanks!
4th Sep 2018, 4:47 PM
HonFu
HonFu - avatar