Jak zrozumieć wskaźniki w C++ czyli proste przykłady działania wskaźników

Jak zrozumieć wskaźniki w C++ czyli proste przykłady działania wskaźników na przykładzie kursu: http://content3.catalog.video.msn.com/e2/ft/share10/7207/0/WinVideo-NativeCoding-Pointers.wmv Projekt uruchamiany pod Visual Studio 2008. [code=cpp] // pointers.cpp : Defines the entry point for the console application. // http://content3.catalog.video.msn.com/e2/ft/share10/7207/0/WinVideo-NativeCoding-Pointers.wmv #include “stdafx.h” #include using namespace std; //============================================================= class CTest { public: CTest() { printf(“CTest constructorn”); } ~CTest() { printf(“CTest deconstructorn”); } }; void RunTest() { cout«“Pierwszy obiekt klasy CTest:n”; CTest test; cout«“Drugi utworzony obiekt klasy CTest:n”; CTest *ptest = new CTest(); delete ptest; // Jeśli nie będzie delete to nie wykona się destruktor dla tego obiektu!

Jak zrozumieć wskaźniki w C++ czyli proste przykłady działania wskaźników na przykładzie kursu: http://content3.catalog.video.msn.com/e2/ft/share10/7207/0/WinVideo-NativeCoding-Pointers.wmv

Projekt uruchamiany pod Visual Studio 2008.

[code=cpp] // pointers.cpp : Defines the entry point for the console application. // http://content3.catalog.video.msn.com/e2/ft/share10/7207/0/WinVideo-NativeCoding-Pointers.wmv

#include “stdafx.h” #include

using namespace std;

//=============================================================

class CTest { public: CTest() { printf(“CTest constructorn”); } ~CTest() { printf(“CTest deconstructorn”); } };

void RunTest() { cout«“Pierwszy obiekt klasy CTest:n”; CTest test; cout«“Drugi utworzony obiekt klasy CTest:n”; CTest *ptest = new CTest(); delete ptest; // Jeśli nie będzie delete to nie wykona się destruktor dla tego obiektu!

}

//============================================================= int _tmain(int argc, _TCHAR* argv[]) { int i = 1; printf(" i : %8.X (value)n", i); // 1 printf(" &i : %8.X (address)n", &i); // adres do i

int *p = &i;
printf("	 p : %8.X (value)n", p); //zawiera adres zmiennej i
printf("	&p : %8.X (address)n", &p); //pokazuje adres wskaznika p
printf("	*p : %8.X (indirection)n", *p); //pokazuje wartosc elementu na ktory pokazuje wskaznik, czyli 1 bo i=1

//referencja
int &r = i;
printf("	 r : %8.X (value)n", r); // 1
printf("	&r : %8.X (address)n", &r); //adres taki sam jak do i bo r jest teraz aliasem ktory pokazuje na i

cout<<"n";

int **pp = &p;
printf("	pp  : %8.X (value)n", pp); // zawiera adres wskaznika p
printf("	&pp : %8.X (address)n", &pp); //adres wskaznika pp
printf("	*pp : %8.X (indirection)n", *pp); //wartosc p, ktora zawiera adres i
printf("	**pp : %8.X (double indirection)n", **pp); //1

cout<<"n========================n";

// wskazniki na tablice
char str[] = "This is a test string";
char *w = str;
printf("	str = "%s"n", str);
printf("	str = %8.X (address)n", str);
printf("	*str = %8.X (indirection)n", *str);
printf("	w = %8.X (value)n", w);
printf("	*w = %8.X (indirection)n", *w);

w[4] = '1';
printf("	str = "%s"n", str);

*(w+4) = '2';
printf("	str = "%s"n", str);


cout<<"n========================n";

//wycieki pamieci
RunTest();


system("pause");
return 0;

} [/code]