10 likes | 152 Views
This C++ code snippet demonstrates the concepts of copies and pointers using simple functions. It showcases how changing values via different methods affects the original variable and its references. The program begins by initializing an integer variable and uses various functions to manipulate its value through references and pointers, illustrating the difference between direct value changes and pointer dereferencing. It concludes with a reset action, highlighting how pointer assignments can affect the data they point to. A helpful resource for beginners learning about memory management in C++.
E N D
Referenser void main (void) { int tal=0; int *tal2=NULL; tal2=&tal; cout<<"change a copy"<<endl; get1(tal); cout<<tal<<" "<<*tal2<<endl; cout<<"change a pointer"<<endl; get2(&tal); //eller get2(tal2); cout<<tal<<" "<<*tal2<<endl; int *tal3=NULL; get2(tal3); cout<<"Reset"<<endl; tal=0; cout<<tal<<" "<<*tal2<<endl; get3(tal); cout<<tal<<" "<<*tal2<<endl; } #include <iostream> using namespace std; void get1(int c) { c=5; } void get2(int* c) { if(c!=NULL) *c=5; } void get3(int& c) { c=7; } change a copy 0 0 change a pointer 5 5 Reset 0 0 7 7