top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What are the advantages of pointer ?

+1 vote
What are the advantages of pointer ?
posted Jan 12, 2015 by Emran

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+1 vote

passing parameter by reference,accessing array elements,dynamic memory allocation,reducing size of parameter

answer Jan 12, 2015 by Gnanendra Reddy
+1 vote

1)Through pointer we can access the address of a variable in other words through pointer we are indirectly accessing variable.

int a = 10;
int *p;
p = &a;
printf("*p = %d\n",*p) \\it should print p = 10,as p is pointing to a;
*p = 20; \\ indirectly accessing the variable;
 printf("*p = %d \n a = %d \n",*p,a); \\ It should print *p = 20 & a = 20; as we have changed the value indirectly;

2) Another use of a pointer is when we want to pass the address of the function then in function (formal argument) it should be used by pointer.

void swap(int *,int *);
int a = 20,b = 30;
printf("before swapping value is ,a=%d & b=%d\n",a,b);  \\it will print a=20,b=30;
printf("after swapping value is ,a=%d & b=%d\n",a,b);  \\it will print a=30,b=20; 
void swap(int *p,int *q)
int temp;
temp = *p;
*p = *q;
*q = temp;

Still there are many more uses of pointer,
i hope i have cleared your doubt

answer Jan 13, 2015 by Chirag Gangdev
+1 vote

Following are few advantages of pointers in C

Pointers provide direct access to memory
Pointers provide a way to return more than one value to the functions
Reduces the storage space and complexity of the program
Reduces the execution time of the program
Provides an alternate way to access array elements
Pointers can be used to pass information back and forth between the calling function and called function.
Pointers allows us to perform dynamic memory allocation and deallocation.
Pointers helps us to build complex data structures like linked list, stack, queues, trees, graphs etc.
Pointers allows us to resize the dynamically allocated memory block.
Addresses of objects can be extracted using pointers

Its not that there are only advantages we have few drawbacks too which can be following
Uninitialized pointers might cause segmentation fault.
Dynamically allocated block needs to be freed explicitly. Otherwise, it would lead to memory leak.
Pointers are slower than normal variables.
If pointers are updated with incorrect values, it might lead to memory corruption.

answer Jan 13, 2015 by Salil Agrawal
Similar Questions
+1 vote

int arr[ ] = { 1, 2 };
p = arr; /* p is pointing to arr */

How pointer p will behave ?

+4 votes

Can someone explain me the usage of function pointer? Probably with real time examples ?

+4 votes

What is the point of declaring Pointer of different types (eg. integer,float,char) as we all know pointer takes 4 bytes of space regardless which type of pointer it is and only contains address?