A Code Snippet To Help Clarify Pointers/References

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

A Code Snippet To Help Clarify Pointers/References

Post by kklouzal »

I had an issue where I would pass a pointer to my classes, change/update the pointer's information and when I went to pass the origional pointer elsewhere the modified information was not there so I created this small bit of code to help me understand what was going on.
I hope it helps someone else!

Code: Select all

// Test Pointers.cpp : main project file.
 
#include "stdafx.h"
#include <iostream>
 
using namespace System;
using namespace std;
 
void conversion(int *b)
{
    cout << "&A -> *B" << "\n\n";
    system("PAUSE");
    *b = 3;
    cout << "&B: " << &b << "\n";
    cout << "*B: " << *b << "\n\n";
    system("PAUSE");
}
 
int main(array<System::String ^> ^args)
{
    int a = 10;
    cout << "A: " << a << "\n";
    cout << "&A: " << &a << "\n\n";
    system("PAUSE");
    conversion(&a);
    cout << "A: " << a << "\n";
    system("PAUSE");
    return 0;
}
This is a CONSOLE Application.
Dream Big Or Go Home.
Help Me Help You.
Post Reply