In which way are the pointers useful? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

In which way are the pointers useful?

17th Apr 2018, 10:58 AM
JøckIłł
JøckIłł - avatar
6 Answers
+ 2
pointers are useful for passing variables by references to functions example:write a function to swap 2 numbers in C void swap(int* a,int* b) { int temp=*a; *a=*b; *b=temp; } the values at memory location where 'a' and 'b' are present are swapped i.e through pointers one can change values at the location pointed to by the pointer! Hope it helps!
17th Apr 2018, 11:39 AM
Abdul Sattar Mapara
+ 2
Pointers are useful when you want to pass the object in. Usually, you would make a copy of the object, but pointers allow you to pass the original one. It can be used if you want to change the values of the original object inside another function or class
17th Apr 2018, 11:28 AM
Ariela
Ariela - avatar
+ 1
Pointers can be used to manage your allocated memory more efficient. For example if you allocate some memory using malloc or calloc you can resize this block using realloc, which makes your program more flexible
17th Apr 2018, 11:41 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 1
JøckIłł its not just about switching values but whenever you want to refer to a,you can do it through the address of 'a' say a's address is to be stored in variable c then int *c=&a ->c is a pointer to an integer ->c contains the address of mem location where value 'a' resides *c means you are dereferencing it and hence accessing the memory location
16th Jun 2018, 7:53 PM
Abdul Sattar Mapara
0
Abdul Sattar Mapara OK I think I've got it... If I've got Int a Int b And I want to switch the values of a and b, then I'll use the pointer Am I right?
17th Apr 2018, 1:39 PM
JøckIłł
JøckIłł - avatar
0
Aaron Eberhardt Sorry I'm just begging... I'm not already used to malloc calloc or realloc ^^'
17th Apr 2018, 1:40 PM
JøckIłł
JøckIłł - avatar