Vemula Sreenath

Saturday, 10 November 2012

Increment and Decrement Operators and Nesting



1.Increment and Decrement Operators
Consider
 i=5;
 j=i++ + i++ + ++i + ++i;
 here on printing j, value is 28
Consider
 i=5;
 j=++i + ++i + ++i + ++i
 here on printing j is 36

 From this we can observe that just count the number of prefix increment and
 then based on that i value multiply the number of i's present in expression
 i.e. in first one,
  there are two ++i's. So, j=4*(i+2)=4*7=28


2.Nesting of Blocks
Consider
 int i=5;
 {
  int i=10;
  printf("%d",i);
 }
 printf("%d",i);

 The variables declared with in block is local to that block and memory frees as soon as control comes out of that block unless it is specified by storage class static or extern.
 there fore output of above program is 10 5

Understanding Pointers


Program Compiled out in Turbo C Compiler...

3.Pointers :
  3.1.Basic Pointers :
   declaring an identifier along with * make it as pointer

   datatype *identifier_name;
  specifying a datatype is not for telling the compiler, how much size to be allocated to the identifier unlike a normal variable But it just specifies the type of aaddress that identifier can hold.
 A pointer is allocated either 2 or 4 or ... bytes depending on the processor irrespective of the datatype

 3.2Assinments to pointers:
  pointer assignment with address of variable:
   Once a pointer is declared it can hold the address of variable of that type.To retrieve address we use & operator.
   like,
   int *p;
   int q;
   p=&q;

 pointer assignment with another pointer:
  A pointer can be assigned to another pointer if they point to same datatype; if they point to different datatype,
  a proper cast is needed. and retrieving the information from the pointer will retrieve the value present at that   location.

 pointer assignment with array :
  A pointer and array are similar with some differences.

 With Integers:
  Consider,
  int a[20]={1,2,3,4};
  int *p;
  p=a;
    a++;//Not possible because a represents the base address and on incrementing it loses it's base address and                it is constant
    p++;//Possible and it can be made point to another array
  int b[20]=[1,2,3,4};
    a=b;//not possible as a is a constant
    p=b;//possible

   With Strings:
     address of any charecter can be assigned to charecter pointer same as arrays.
    char *p="that";
    char s[]="this";
     s="hhhh";//Not possible as s is constant
     p="hhhh";//possible
    s[0]='h';//possible as memory allocated for the string constant acts as volatile
    *p='h';//not possible as memory aloocated for the string constant acts as const

    With const keyword:
      const char *p="this";//same as above p is not constant and it can point to other string constant  but the data                                             it point to cannot be altered like
                        *p='g';//not possible
                         p="that";//possible
                        *p='f';//again not possible
        char *const p="that";
         now p="this" will result in an error as p is made constant p cannot point to any other string constant.

     Assignment with integers:
      Only Zero is considered to be valid integer value for pointer. and a pointer can be assigned with 0.

Special Kind of pointers:
  another type of pointer called void pointer can be declared.
  void *p;
  p is of type void meaning it point to nothing. pointer of any datatype canbe casted to this pointer and the  resultant type and value will be of the one with which it is assigned with.

Arthemetics on pointers:
  except +,- no other arthemetic operation can be performed on pointers.
  the addition can be performed along with another integer constant and
  the subraction can be performed along with integer constant and with another pointer if they point to same  array.

Other operations:
  all relational and logocal operations such as <,<=,>,>=,==,!= can be performed with pointer with another pointer. and also Comparision can be done between pointer and integer constant (Zero);

pointers to arrays:
  a pointer can be made point to multi dimensional arrays.
 ex:  int (*p)[10];
   p can point to two dimensional of [*][10] as we know , we can declare a array without specifing the row at initilization time and * indicates it can have any number of rows and a fixed number of column size of 10.same rule applicable to pointer with p point to two dimensional array column sixe 10 irrespective of row size.

 ex: int (*p)[10]
 Array of pointer

Pointers with functions:
  the way a normal variable have address , functions will also have address abd we can have pointer to it.
  Simple example of pointer pointing to a "printf"   function
  Syntax of printf:
  int printf(char *,...);
  so to point  it,the pointer should also return same

Wednesday, 7 November 2012

C Programming eBooks

People who are familiar with C Programming basic concepts can refer to
"The C Programming Language 2nd Ed by K & R"
http://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628

"C Programming A Modern Approach 2nd Ed by K.N.King". This book contains all the basics of C
http://www.amazon.com/Programming-Modern-Approach-2nd-Edition/dp/0393979504


Complicated Pointer Declarations

This is my first posting on pointers.
Hope you would see series of related posts on pointers.
For any query on C language, send queries to 

This is from The C Programming Language - 5.12
1. int  (*(*x[3])( ))[5];
  What is The meaning of this declaration??
 Sol: 
    Understanding the above declaration is easy once we follow this order.. the inner most loop to the outer most one.
  a. consider *x[3];
    which means we are creating three array of pointers
  As these are pointers, to say the type to which they point to we made the outer declaration.
 b. in general , int (*p())[5];
   means p is a function which returns pointer to array with column size as 5.
  on Combining a,b 
   we can say above declaration as 
  the array of pointers, each storing the address of the function returning pointer to an array of column size as 5.