What is the difference between & and * pointers?


Flag Post

Batman

asked
2

What is the difference between & and * pointers?

1 Answers


[{"insert":"Hope this example explains it a little bit:\n#include <iostream>"},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"using namespace std;"},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"int main() {"},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"    int a = 0; // \"a\" is an integer"},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"    "},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"    cout << &a << endl; // print address of \"a\""},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"    cout << *&a; // print value at the address of \"a\" -> 0"},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"}"},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"Basically, "},{"attributes":{"bold":true},"insert":"&"},{"insert":" refers to the address of a variable and"},{"attributes":{"bold":true},"insert":" * "},{"insert":"de-references (fetches the data) at the address specified.\nHere is a link to a project where you can play around with the code a little bit: https://codeassist.org/projects/5ba2834711a2e46ed3e4f91d/\n"}]

Your Answer


Feedback