Posts

Post/Pre Increment/Decrement ---------------------> Boon or Curse!

 Ah,  so, We love Increment/Decrement operators ( well most of us ), don't we?? Be Cautious while using INCREMENT/DECREMENT operators when: 1) While Giving ARGUMENTS to a RECURSIVE FUNCTION: Let's take an example of a recursive factorial() int fact(int n) {     if(n==1)      return 1;      return n*fact(n--); } The above function may look straightforward but, the above function will call itself infinitely. Why? We have used post decrement ( -- ) while passing value to fact(). Lets recall, how does POST DECREMENT work? --> It first assigns the value then decrement occurs. so, we actually passed  N  instead of ( N-1 ) in argument, thus resulting in infinite recursion.  We can instead use PRE DECREMENT OPERATOR. Note: NEVER USE POST DECREMENT/INCREMENT OPERATORS while passing values to a function. 2) Don't use POST/PRE INCREMENT OPERATORS at all, when the recursion is not tail recursion: Consider the below two functions: int calc1(int a,int b) {     if(a>b)         retu

Why typedef is handy ?

Consider this.... If you want two pointer variables ( lets say of type int * ), how will you declare it? -->   int* ptr1,ptr2;            #WRONG If we follow the above syntax, the compiler will allocate 2 space ( i )  a pointer variable ptr1 which will store an address of INTEGER VARIABLE ADDRESS. ( ii )  a normal variable  ptr2 which will store an INTEGER VALUE. To avoid this , METHOD 1:  int * ptr1, *ptr2; or ,  Use Typedef . : It is used to give alternate name to your data types. Syntax:    typedef  data_type   alternate_name_for_that_datatype; eg, typedef int* intPointer; this way, you can declare  intPointer ptr1,ptr2; Now, consider this, Suppose you are working with Linked Lists or any other ADTs, or structures,  You must have noticed, those data types name can become lengthy  ie, struct node, struct myPersonalInformation, etc. now, consider reserving a space for those datatypes. ie, struct myPersonalInformation name1; What if u could avoid all that??... typedef struct myPers