Thursday, December 29, 2011

Accident of syntax !!

Once,I read in a book-"A C programmer without knowledge of pointers is like a fish which doesn't know how to swim".After this post,actually, you will be able to appreciate this.Basically,i'll be dicussing a very simple operation in C, i.e scanning & printng an array,rather a 2-D array.I'll discuss about 3-4 ways in which we can use pointers to print a 2-D array,coz today,if you know only: printf("%d ",a[i][j]);  to print a 2D-array,u need to improve man!.

So,starting with scanning a 2-D array(i'll be talking of an array consisting of int elements):
scanf("%d",&a[i][j]);
now,I don't know that why to use this '&' operator in scanning,while we know that the name of the array stores the beginning address of that array, and we can access all the elements of that array manupulating the name.What about this:
int a[3][2];
 for(int i=0;i<3;i++)
 {
  for(int j=0;j<2;j++)
   scanf("%d",*(a+i)+j);
 }
as we all know, a 2-D array consists of a series of 1-D arrays.Here, *(a+i) takes the pointer to the successive 1-D ararys and *(a+i)+j is incremented accordingly to scan successive integers. 
here,a-'name of the array',acts like a special pointer,which on incrementing 'i' by 1,points to the next 1-D array.
Basically,*(a+i) is equivalent to a[i],i.e ith element of that 2-D array,which actually refers to ith 1-D array, so a[i] gives the base address of ith 1-D array and '+j' refers to the address of  the jth element after the first element.
Now comes the printing:
Using above method,we know how to point to address of each element of a 2-D array,so we can also print their values using 1 more "accident of syntax"!, i.e. '*' operator (It was described by Ritchie himself as such).
 for(int i=0;i<3;i++)
 {
         for(int j=0;j<2;j++)
              printf("%d",*(*(a+i)+j));
 }

There is an another method:
for(int i=0;i<2;i++)
{
         for(int j=0;j<3;j++)
             printf("%d ",*(((int *)a)+i*3+j));
}
I think this 'i*3+j' is clear.Main point to notice here is that I m typecasting 'a' to an int pointer.I need to do this because as I mentioned earlier,on incrementing a, it points to the next 1-D array, but here,we want it to increase int by int.
This is my favourite method.

The next method is kind of a extension of this method:
for(int i=0;i<2;i++) 
{
         int *p=a+i;
         for(int j=0;j<3;j++)
               printf("%d",*(p+j));
}
here,instead of typecasting 'a' into an int pointer,I m originally taking an int pointer, and in every iteration,I m copying the base address of the corresponding 1-D array in that int pointer and then incrementing it to successive integers in that 1-D array.
In C,u need to have a good control over changing data-types of pointers.

So,these were few techniques to make your programme more interesting.
Hope,this is helpful..:)


hostgator coupon 2012

Saturday, December 24, 2011

Using just 1 line to copy a string

I dont know, where on this earth, that strcpy func is considered good..!!
when we can copy 1 string to another in just 1 line..

Below is a sample code(line) to copy the contents of string 's1' to string 's2':

while(*s2++=*s1++);

yes,i m using a ';' jst after the while loop.
Actually,the fact is that a ';' jst after any loop is equivalent to '{ }', that is "empty curly braces".
So,the loop will run till the value at the address s1 is a NULL and copy the same character to the address contained in s2.
The post-increment operator first assigns the value at address contained in s1 to address contained in s2 and then increments the address stored in s1,pointing to the next character, till NULL is encountered.
Until a NULL is encountered, the assignment statement returns a non-zero value to the while and it runs.As the same is encountered,the condition returns a ZERO value to the while and the loop terminates.
but yes,the last assignment operation is done here,that's
when  *s1='\0' or *s1=0; (as you all know, ASCII value of '\0' is 0), it is assigned to the present address stored in s1;

Without this last assignment, s2 is not a complete string.In fact,it is not even a string, it's simply a character array.
Now, what's exactly the difference between both of them, that i'l xplain in some other post.

And u have to note 1 imp point here:
u can increment the base address of a string like this only in a function,not in main(think why??).
If u want to perform this operation in main,u have to use char pointers,pointing to both the strings, like:
char *s1="yash";
char *s2; 

hope this post is helpful :)


 




hostgator coupon 2012