Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example showing difference between pointer-to-pointer and reference-to-pointer in C++
// Example that shows difference between pointer-to-pointer
// and reference-to-pointer.
#include <cstdio>
void func_ptr(int **x)
{
*x = new int;
**x = 100;
}
void func_ref(int * &x)
{
x = new int;
*x = 99;
}
int main()
{
// Pointer-to-pointer
{
int *xp = nullptr;
func_ptr(&xp);
printf("%d\n", *xp);
delete xp;
}
// Reference-to-pointer
{
int *xp = nullptr;
func_ref(xp);
printf("%d\n", *xp);
delete xp;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.