0
In which way are the pointers useful?
6 Respostas
+ 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!
+ 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
+ 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
+ 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
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?
0
Aaron Eberhardt
Sorry I'm just begging... I'm not already used to malloc calloc or realloc ^^'