Pages

Wednesday, February 5, 2014

RESOLVING C DECLARATIONS

ALGORITHM FOR RESOLVING C DECLARATION

OPERATORS: * & () [] ,
PRECEDENCE (DECREASING ORDER): () [] * & ,
ASSOCIATIVITY (POSTFIX): () [] 
ASSOCIATIVITY (PREFIX): * &

Meaning: 
   [] : array of
   () : function taking ... and returning 
   *  : pointer to 
   &  : reference to {address of}
   ,  : and
1. FIND THE IDENTIFIER
2. RESOLVE THE OPERATORS ON THE RIGHT. EG: () , []
   IF () THEN RESOLVE THE FUNCTION ARGUMENTS
3. RESOLVE OPERATORS (EG: *) and TYPES ON THE LEFT
4. REPEAT 2 & 3 TILL NOTHING LEFT UNRESOLVED
Few Examples Of Resolving Complex Declaration. References: Clockwise Spiral Rule

Monday, February 3, 2014

UNCOMPLICATE COMPLEX DECLARATION

COMPLEX DECLARATION IN C

int (*ptr)[100];
int (*ptr[100]);
int (*ptr)();
int (*ptr[100])();

Evaluating Complicated DECLARATION

- int *ptr[100];
   
   * *ptr[100] integer
   * *(ptr is array of 100) integer
   * ptr is array of pointer to integer
- int (*ptr)[100];
   
   * (*ptr)[100] integer
   * (ptr is pointer to) array of 100 integer
   * ptr is pointer to array of integer
- int *(ptr[100]);
  
   * *(ptr[100]) integer
   * *(ptr is array of 100) integer
   * ptr is array of pointer to integer
- int (*ptr[100]);
   
   * *ptr[100] integer
   * *(ptr is array of 100) integer
   * ptr is array of pointer to integer   
- int *func();
   
   * *func() integer
   * *(func is function returning) integer
   * func is function returning pointer to integer
- int (*func)();

   * (*func)() integer
   * (func is pointer to)function returning integer
   * func is pointer to function returning integer
- int (*func[])()
   
   * (*func[])() integer
   * (*(func is array of))function returning integer
   * *(func is array of) function returning integer
   * func is array of pointer to function returning integer
- int *(*f)()
   
   * *(*f)() integer
   * *((*f) function returning) integer
   * *(f is pointer to function returning) integer
   * f is pointer to function returning pointer to integer
- int *(*f[])()

   * *(*f[])() integer
   * *(*(f is array of))() integer
   * *(f is array of pointer to)() integer
   * *(f is array of pointer to function returning) integer
   * f is array of pointer to function returning pointer to integer
- int (*(*x())[])()

   * (*(*x())[])() integer
   * (*(*(x is function returning))[])() integer
   * (*(x is function returning pointer to)[])() integer
   * (*(x is function returning pointer to array of))() integer
   * (x is function returning pointer to array of pointer to)() integer
   * x is function returning pointer to array of pointer to function returning integer
- int (*(*x[3])())[5]
  
   * (*(*x[3])())[5] integer
   * (*(*x is array of)())[5] integer
   * (*(x is array of pointer to)())[5] integer
   * (x is array of pointer to function returning pointer to)[5] integer
   * x is array of pointer to function returning pointer to array of integer
- void (*f())(int)
  
   * (*f())(int) void
   * (*(f is function returning))(int) void
   * (f is function returning pointer to) (function taking integer and returning) void
   * f is function returning pointer to function taking integer and returning void
- void (*)(int)
  
   * (*)(int) void
   * (pointer to)(function taking integer and returning) void
   * pointer to function taking integer and returning void
- void *(int)

   * *(int) void
   * (function taking integer and returning) (pointer to) void
   * function taking integer and returning pointer to void

- void *f(int, void (*)(int))

   * *f(int, void (*)(int)) void
   * *(f is function taking integer and pointer to function taking integer and returning void and returning) void
   * f is function taking integer and pointer to function taking integer and returning void and returning pointer to void


- void (*f)(int, void (*)(int))
   
   * (*f)(int,void (*)(int)) void
   * (f is pointer to)(function taking integer and pointer to function taking integer and returning void and returning) void
   * f is pointer to function taking integer and pointer to function taking integer and returning void and returning void


- void (*f(int, void (*)(int)))(int)
   
   * (*f(int, void (*)(int)))(int) void
   * (*(f is function taking int and pointer to function taking integer and returning void and returning))(int) void
   * f is function taking int and pointer to function taking integer and returning void and returning pointer to function taking integer and returning void
   

- float (*(*b())[])()
  
   * (*(*b())[])() float
   * (*(*(b is function taking void and returning))[])() float
   * (*(b is function taking void and returning pointer to)[])() float
   * (*(b is function taking void and returning pointer to array of))() float
   * (b is function taking void and returning pointer to array of pointer to)() float
   * b is function taking void and returning pointer to array of pointer to function taking void and returning float


- void *(*c)(char, int (*)())

   * *(*c)(char, int (*)()) void
   * *(c is pointer to)(char, int (*)()) void
   * *(c is pointer to function taking char and pointer to function taking integer and returning void and returning) void
   * c is pointer to function taking char and pointer to function taking integer and returning void and returning pointer to void


- char **(*f)(char *, char **)

   * f is pointer to function taking pointer to char and pointer to pointer to char and returning pointer to pointer to char

- void **(*d)(int &, char **(*)(char *, char **))
   
    * **(*d)(int &, char **(*)(char *, char **)) void
    * **(d is pointer to)(int &, char **(*)(char *, char **)) void
    * **(d is pointer to function reference to integer and pointer to function taking pointer to char and pointer to pointer to char and returning pointer to pointer to char and returning) void
    * d is pointer to function reference to integer and pointer to function taking pointer to char and pointer to pointer to char and returning pointer to pointer to char and returning pointer to pointer to void


- float (*(*e[10])(int &))[5]

   * (*(*e[10])(int &))[5] float
   * (*(*(e is array of))(int &))[5] float
   * (*(e is array of pointer to)(int &))[5] float
   * (*(e is array of pointer to function taking reference to integer and returning))[5] float
   * (e is array of pointer to function taking reference to integer and returning pointer to)[5] float
   * (e is array of pointer to function taking reference to integer and returning pointer to array of) float
   * e is array of pointer to function taking reference to integer and returning pointer to array of float


- float *(*(*foo())[SIZE][SIZE])()
   
   * *(*(*foo())[SIZE][SIZE])() float 
   * *(*(*foo is function taking void and returning)[SIZE][SIZE])() float 
   * *(*(foo is function taking void and returning pointer to)[SIZE][SIZE])() float 
   * *(*(foo is function taking void and returning pointer to multi-dimensional array of))() float 
   * *(foo is function taking void and returning pointer to multi-dimensional array of pointer to)() float 
   * *(foo is function taking void and returning pointer to multi-dimensional array of pointer to function taking void and returning) float 
   * foo is function taking void and returning pointer to multi-dimensional array of pointer to function taking void and returning pointer to float 


Friday, January 31, 2014

RESOLVING: POINTER TO ARRAY AND ARRAY OF POINTER

HOW TO RESOLVE POINTER TO ARRAY OR ARRAY OF POINTER

int *ptr[100];
int *(ptr[100]);
int (*ptr[100]);
int (*ptr)[100];

ARRAY OF POINTERS

int *ptr[100];
int (*ptr[100]);
int *(ptr[100]);

POINTER TO ARRAY

int (*ptr)[100];

REVERSE SINGLY LINKED LIST

typedef struct _listElement {
    int nData;
    struct _listElement* pNext;
} LIST;

/* Reversing A Linked List Recurrsively */
void recurrsivereverse(LIST** pHead) {
    LIST* pCurrent = *pHead;
    LIST* pNext = NULL;
    
    if(pCurrent == NULL) {
        return;
    }
    pNext = pCurrent->pNext;
    recurrsivereverse(&pNext);
    if(pNext)
    {
        pCurrent->pNext->pNext = pCurrent;
        pCurrent->pNext = NULL;
        *pHead = pNext;
    }
    return;
}

/* Reversing A Linked List Iteratively */
void iterativereverse(LIST** pHead) {
    LIST* pCurrent = *pHead;
    LIST* pPrev = NULL;
    LIST* pNext = NULL;

    while(pCurrent) {
        pNext = pCurrent->pNext;
        pCurrent->pNext = pPrev;
        pPrev = pCurrent;
        pCurrent = pNext;
    }
    
    *pHead = pPrev;
    return;
}

Monday, January 27, 2014

INFIX TO PREFIX

GET THE INFIX EXPRESSION IN A BUFFER
INITIALIZE PRINT QUEUE
INITIALIZE OPERATOR STACK
REVERSE THE INFIX EXPRESSION BUFFER
WHILE BUFFER IS NOT EMPTY
   READ A TOKEN FROM THE BUFFER
   IF THE TOKEN IS A OPERAND
       ENQUEUE THE TOKEN IN PRINT QUEUE
   ELSE IF THE TOKEN IS A OPERATOR
       IF OPERATOR STACK EMPTY
          PUSH THE TOKEN IN THE OPERATOR STACK
       ELSE
          IF TOS (OPERATOR STACK) IS HIGHER PRIORITY OPERATOR THAN TOKEN
              ENQUEUE ( POP FROM OPERATOR STACK) IN PRINT QUEUE
              PUSH THE TOKEN IN THE OPERATOR STACK
          ELSE
              PUSH THE TOKEN IN THE OPERATOR STACK
          END IF
       END IF
   ELSE IF THE TOKEN IS ")"
       PUSH THE TOKEN IN THE OPERATOR STACK
   ELSE IF THE TOKEN IS "("
        IF OPERATOR STACK IS EMPTY
            INFIX EXPRESSION IS NOT VALID
        ELSE
            WHILE TOS (OPERATOR STACK) IS NOT ")" AND OPERATOR STACK NOT EMPTY
                ENQUEUE ( POP FROM OPERATOR STACK) INTO PRINT QUEUE
            END WHILE
            IF OPERATOR STACK EMPTY
                INFIX EXPRESSION IS NOT VALID
            ELSE
                POP FROM OPERATOR STACK /* POP ")" */
            END IF
        END IF
   END IF
END WHILE
WHILE OPERATOR STACK IS NOT EMPTY
    ENQUEUE ( POP FROM OPERATOR STACK) INTO PRINT QUEUE
END WHILE
REVERSE THE PRINT QUEUE

Sunday, January 26, 2014

INFIX TO POSTFIX

GET THE INFIX EXPRESSION IN A BUFFER
INITIALIZE PRINT QUEUE
INITIALIZE OPERATOR STACK
WHILE BUFFER IS NOT EMPTY
    READ A TOKEN FROM THE BUFFER
    IF TOKEN IS OPERAND
       ENQUEUE THE TOKEN IN PRINT QUEUE
    ELSE IF TOKEN IS A OPERATOR
        IF OPERATOR STACK IS EMPTY
            PUSH THE TOKEN IN THE OPERATOR STACK
        ELSE
            IF THE TOS (OPERATOR STACK) IS HIGHER PRIORITY OPERATOR THAN TOKEN
                ENQUEUE ( POP FROM OPERATOR STACK) INTO PRINT QUEUE
                PUSH THE TOKEN IN THE OPERATOR STACK
            ELSE
                PUSH THE TOKEN IN THE OPERATOR STACK
            END IF
        END IF
    ELSE IF TOKEN IS "("
        PUSH THE TOKEN IN THE OPERATOR STACK
    ELSE IF TOKEN IS ")"
        IF OPERATOR STACK IS EMPTY
            INFIX EXPRESSION IS NOT VALID
        ELSE
            WHILE TOS (OPERATOR STACK) IS NOT "(" AND OPERATOR STACK NOT EMPTY
                ENQUEUE ( POP FROM OPERATOR STACK) INTO PRINT QUEUE
            END WHILE
            IF OPERATOR STACK EMPTY
                INFIX EXPRESSION IS NOT VALID
            ELSE
                POP FROM OPERATOR STACK /* POP "(" */
            END IF
        END IF
    END IF
END WHILE
WHILE OPERATOR STACK IS NOT EMPTY
    ENQUEUE ( POP FROM OPERATOR STACK) INTO PRINT QUEUE
END WHILE