understanding pointers

i want to know what is the basic benefit of pointers? and which program is better if in one program we pass an array to a function and in other one we pass a reference of the first element of the array and both program produce same output?
i also want to know what happen if we increment a pointer (say p) as p++ and also if before increment it points to an integer variable (say int a = 2) than after increment where it points?
and it is also helpful if someone explain the basic concept of pointer function.

Pointers have their own use.

Passing array as reference, or a pointer to first element of array, both should give you same result.

The real benefit of pointer is seen in methods like two pointer methods and Data structure like trees.

At basic level, you will rarely use pointers. But when you get to advanced Data structures to solve Q, or study some data structures like Linked List etc. you will see how pointers make life simple there. So, its just about time.

The basic concept of pointer is that, instead of assigning values to variables, we save the memory location in the variable. The variable then, can be taken as, “It has the address when this value is stored”
The value can be accssed by using '*' operator. Eg-

int b= *a;//Assuming a is pointer.

Lets say i give you a question, like-

You have to make a function “Change (int a, int b)” such that-

  1. It takes a and b as paramenter from main.
  2. If a and b both are odd, then their values are doubled
  3. If a and b both are even, then they are multipled with itself ( meaning a becomes a x a= a ^2 )
  4. If one is odd and other is even, then double the even one and triple the odd one.

Lets say our main program is like-

int main()
   int a=3,b=5;
   printf ("%d %d", a,b);
   return 0

Its clear that we want the variables a and b to be changed in the function. But, if we dont use pointers, then you must have read that they will get “passed by value” (a copy will be created, changes will occur in copy, and at end of function copy gets destroyed. Original values unaffected).

Here, to reflect changes in the variables, we will use pointers to pass by reference. That will make any change happening in the function to reflect in original variables.

Coming you your increment Q, there are 2 cases-

Case 1:

Lets say pointer is pointing to FIRST element of the array. Now we use p++, then it will point to SECOND element of array. Similarly goes on. If it crosses array size, a random garbage value is given/may cause undefined behavior (such pointers are dangerous. I am NOT SURE about undefined behavior tho.)

Case 2:

If a is a variable and p is pointing to it, then p++ will make it point…anywhere…we cant say. It will give a garbage value. Similar to case in array when we go out of bounds. It will most probably point to a garbage value. Cant say for undefined behavior, but its best to beware.

thanks bro. it is very helpful to me and i hv also wonder that this is anyhow related to TLE?

Using pointers shouldnt not cause TLE. But in practice, we see lots of incorrect/faulty implementation will lead to a bug or another. But in the end, only playing with them will help you learn ^^