Pointers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 10

Pointers

I understand that pointers point to a memory address, but what do they really do? can they access out of scope variables? ex: why would i: int x = 5; int *ptr = &x; cout << *ptr << endl; and not: int x = 5; cout << x << endl;

13th Aug 2018, 12:45 AM
Harry
Harry - avatar
33 Answers
+ 16
Hazer C++ Begginer You could, yes. But imagine you are working on a large code, thousands of lines. It is good practice to make your code modular, that is, isolate certain functionalities in functions. By writing functions you can reutilize code in several parts of a program without having to write it again. This does not successfully swaps the values held in the variables: void swap(a, b) { int tmp = a; a = b; b = tmp; } If you try this in the main function: int x = 3; int y = 5; swap(x, y); Then 3 is still in "x" and 5 is still in "y". Because only the values of the variables were passed to the swap function, not the addresses.
13th Aug 2018, 1:48 AM
Eduardo Petry
Eduardo Petry - avatar
+ 9
Hazer C++ Begginer you use pointers when you need to pass variables by reference and to manipulate the data in specific memory locations, and in general that's it. I think the reason why people stress over pointers is because it is a concept that takes some time to fully "sink in".
13th Aug 2018, 2:31 AM
Eduardo Petry
Eduardo Petry - avatar
+ 8
You can pass variables by reference when using pointers. A classic example is a function to swap the values of two variables. void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; } This function successfully swaps the values in memory, because the addresses of the variables are passed to it. int x = 3; int y = 5; swap(&x, &y); Now 5 is stored in "x" and 3 is in "y".
13th Aug 2018, 1:35 AM
Eduardo Petry
Eduardo Petry - avatar
+ 4
so when i call a function like this it changes the return variable to whatever i type when i call it? I just dont know how it knew that you were talking about temp when you called it Ketan Lalcheta
15th Aug 2018, 5:57 AM
Harry
Harry - avatar
+ 3
Hazer C++ Begginer Not necessarily only for "organization". That's just the case for the swap function. Passing by reference means you are altering the values in the variables which are outside of the scope of the function. I recommend you read more on passing by reference: http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/
13th Aug 2018, 2:39 AM
Eduardo Petry
Eduardo Petry - avatar
+ 3
Hazer C++ Begginer pointer is a life saver when you deal with big amount of data... void print(int x) { cout << x << endl; } void print(int* x) { cout << *x << endl; } consider above example.. you are using pointer in second case while normal variable in first case... if you call function having pointer as argument, new copy of variable is not created and memory address of variable itself (which you declared in main function) is passed to function... where as in normal function, new variable is created which is local to that function only... compiler has to create a copy of variable in function and need to destroy the same at the end of function call. so, effectively you are creating less number of variable by directly accessing same variable through address of variable using pointer... this can be done using reference as well in c++ but reference has some different limitations compared to pointer..
13th Aug 2018, 5:02 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 3
Ketan Lalcheta How does that work? you arent even modifying the temp variable?
15th Aug 2018, 5:50 AM
Harry
Harry - avatar
+ 3
Hazer C++ Begginer in this scenario, you will not appreciate importance of function returning reference... sometimes, it is must to return reference... case I recall for same is << operator overloading for object cout
15th Aug 2018, 7:42 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 3
I don't understand this pointer lesson .can enybody define that lesson to me please..
27th Jan 2020, 4:47 PM
Thisari Kanchana
Thisari Kanchana - avatar
+ 2
Hazer C++ Begginer making it global violates oops concepts... any one can access it from any class... you should wrap your data and functions to avoid data being accessed by unauthorised access.. that's the reason we use encapsulation concept... data as private or protected and member function to access those data as public...
13th Aug 2018, 5:12 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
Hazer C++ Begginer getvariable() = "Sololearn"; is replaced as temp = "Sololearn"; as function returns variable (not value)
15th Aug 2018, 5:54 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
Hazer C++ Begginer I think I got your confusion... let me give you another better example: Use below code and observe output that temp1 is updated after function call: string temp1 = "Test1"; string temp2 = "Test2"; string& getVariable() { return temp1 ; } int main() { //before function call cout << "temp1 is " << temp1 << endl; cout << "temp2 is " << temp2 << endl; getVariable() = "Updated"; //after function call cout << "temp1 is " << temp1 << endl; cout << "temp2 is " << temp2 << endl; return 0; } Now change a line in getvalue function with return temp2 instead of temp1... observe output now and you may see that temp2 value gets updated..
15th Aug 2018, 6:05 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
so it changes the return value? would that be the same as taking a parameter and changing the string to that parameter? Ketan Lalcheta
15th Aug 2018, 6:09 AM
Harry
Harry - avatar
+ 2
as in normal function, we display int x = getsum(); cout << x; displays value returned by function, reference returns variable from function...
15th Aug 2018, 6:10 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
i think i fixed it Ketan Lalcheta
15th Aug 2018, 6:20 AM
Harry
Harry - avatar
+ 2
but if it does the same thing, why use one over the other? Ketan Lalcheta
15th Aug 2018, 6:37 AM
Harry
Harry - avatar
+ 2
Someone,can u explain to me what is the real purpose of the pointer????
22nd Dec 2019, 12:29 PM
Akmal Hakimi
Akmal Hakimi - avatar
+ 2
I don't understand about the location memory and all
22nd Dec 2019, 12:30 PM
Akmal Hakimi
Akmal Hakimi - avatar
+ 1
but couldnt i just do int main() { int x = 5; int y = 10; int tmp = x; x = y; y = tmp; }
13th Aug 2018, 1:41 AM
Harry
Harry - avatar
+ 1
but isnt string temp; string &getstring() { return temp; } int main() { getstring() = "test"; return 0; } the same as string getstring(string input) { return input; } int main() { getstring("hello"); return 0; }
15th Aug 2018, 6:17 AM
Harry
Harry - avatar