<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1075957451022844053</id><updated>2012-01-14T14:56:59.317-08:00</updated><category term='C snippet'/><category term='csh'/><category term='bzero'/><category term='Pointers'/><category term='memset'/><category term='Ubuntu'/><category term='Let Us C'/><category term='C Tutorial'/><category term='goto'/><category term='ebook'/><category term='Hello World'/><category term='sizeof'/><title type='text'>C-Unix-Linux</title><subtitle type='html'>Alongwith sharing my knowledge,I also intend to answer your queries related to C Unix and Linux,you can send me the queries at my personal email-ID: sarsij.nayanam@gmail.com, also you can drop a comment asking you questions....some helpful will definitely answer your questions in a much shorter time. Happy Reading and Programming :-)</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://c-unix-linux.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://c-unix-linux.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Sarsij Nayanam</name><uri>https://profiles.google.com/108798285946939476460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-Et8dADNkSFA/AAAAAAAAAAI/AAAAAAAAB-k/_Ygfe7sXRto/s512-c/photo.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>11</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1075957451022844053.post-2922917116512945557</id><published>2009-05-22T06:10:00.000-07:00</published><updated>2009-05-22T06:13:31.009-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C snippet'/><category scheme='http://www.blogger.com/atom/ns#' term='C Tutorial'/><title type='text'>Arrays and Pointer to arrays : One Dimension</title><content type='html'>&lt;span class="Apple-style-span" style="border-collapse: collapse; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt;In this snippet we will look at manipulating pointers to single-dimensional arrays with ease. For this we first need to review one-dimensional arrays.Subsequent snippet will cover multidimensional arrays.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;***************************&lt;br /&gt;Manipulating simple arrays&lt;br /&gt;***************************&lt;br /&gt;&lt;br /&gt;Suppose we have the following two declarations&lt;br /&gt;&lt;br /&gt;int array[10];&lt;br /&gt;&lt;br /&gt;and&lt;br /&gt;&lt;br /&gt;int * parray[10];&lt;br /&gt;&lt;br /&gt;Diagrammatically, the difference between the two arrays  is as shown below&lt;br /&gt;two figures.&lt;br /&gt;&lt;br /&gt;Figure-1:&lt;br /&gt;&lt;br /&gt;              +——————————+&lt;br /&gt;array [0]      |   int    |&lt;br /&gt;              +——————————+&lt;br /&gt;array [1]      |   int    |&lt;br /&gt;              +——————————+&lt;br /&gt;array [2]      |   int    |&lt;br /&gt;              +——————————+&lt;br /&gt;array [3]      |   int    |&lt;br /&gt;              +——————————+&lt;br /&gt;array [4]      |   int    |&lt;br /&gt;              +——————————+&lt;br /&gt;array [5]      |   int    |&lt;br /&gt;              +——————————+&lt;br /&gt;array [6]      |   int    |&lt;br /&gt;              +——————————+&lt;br /&gt;array [7]      |   int    |&lt;br /&gt;              +——————————+&lt;br /&gt;array [8]      |   int    |&lt;br /&gt;              +——————————+&lt;br /&gt;array [9]      |   int    |&lt;br /&gt;              +——————————+&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Figure-2:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;             +——————————+         +——————————+&lt;br /&gt;parray[0]     |  int *   |  ——————&gt;|   int    | *parray[0]&lt;br /&gt;             +——————————+         +——————————+&lt;br /&gt;+——————————+&lt;br /&gt;parray[1]     |  int *   |  ——————————————————————————————&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt;&lt;wbr&gt;———&gt;  |   int&lt;br /&gt;| *parray[1]&lt;br /&gt;             +——————————+           +——————————+&lt;br /&gt;+——————————+&lt;br /&gt;parray[2]     |  int *   |  ————————&gt;|   int    | *parray[2]&lt;br /&gt;             +——————————+           +——————————+              +——————————+&lt;br /&gt;parray[3]     |  int *   |  ——————————————————————————————&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt;&lt;wbr&gt;———&gt; |   int    |&lt;br /&gt;*parray[3]&lt;br /&gt;             +——————————+          +——————————+               +——————————+&lt;br /&gt;parray[4]     |  int *   |  ——————&gt; |   int    | *parray[4]&lt;br /&gt;             +——————————+          +——————————+&lt;br /&gt;+——————————+&lt;br /&gt;parray[5]     |  int *   |  ——————————————————————————————&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt;&lt;wbr&gt;—————&gt;|   int&lt;br /&gt;| *parray[5]&lt;br /&gt;             +——————————+           +——————————+&lt;br /&gt;+——————————+&lt;br /&gt;parray[6]     |  int *   |  ————————&gt;|   int    | *parray[6]&lt;br /&gt;             +——————————+           +——————————+              +——————————+&lt;br /&gt;parray[7]     |  int *   |  ——————————————————————————————&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt;&lt;wbr&gt;———&gt; |   int    |&lt;br /&gt;*parray[7]&lt;br /&gt;             +——————————+          +——————————+               +——————————+&lt;br /&gt;parray[8]     |  int *   |  ——————&gt; |   int    | *parray[8]&lt;br /&gt;             +——————————+          +——————————+&lt;br /&gt;+——————————+&lt;br /&gt;parray[9]     |  int *   |  ——————————————————————————————&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt;&lt;wbr&gt;———&gt;  |   int&lt;br /&gt;| *parray[9]&lt;br /&gt;             +——————————+&lt;br /&gt;+——————————+&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;           int * implies int pointer&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the first declaration we access the ith int in the array by performing array[i]. In the second declaration, we access the ith integer (though this int is not part of the array) by performing     *parray[i]., In the second declaration, the ith int pointer (which are elements of the array parray) can be accessed by performing  parray[i] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;***************************&lt;br /&gt;Dual Nature of array names:&lt;br /&gt;***************************&lt;br /&gt;&lt;br /&gt;Array names have dual nature. Sometimes it signifies the whole array. That&lt;br /&gt;is why, given&lt;br /&gt;&lt;br /&gt;TYPE  array_name[M];&lt;br /&gt;&lt;br /&gt;We can find the size of the array by printing out  sizeof (array_name). For&lt;br /&gt;example:&lt;br /&gt;&lt;br /&gt;Given&lt;br /&gt;&lt;br /&gt;long     array_of_long[100] ;&lt;br /&gt;&lt;br /&gt;Then, the size of array  array_of_long  is given by  sizeof(array_of_long.&lt;br /&gt;Hence the array name signifies the whole array.&lt;br /&gt;&lt;br /&gt;At other times the array name behaves as a pointer. The C compiler creates&lt;br /&gt;a  pointer whose name is again the array name itself. And this points to&lt;br /&gt;the first element of the array. Thus given:&lt;br /&gt;&lt;br /&gt;TYPE array_name[M];&lt;br /&gt;&lt;br /&gt;Then array_name points to the first element, i.e.  array_name[ 0 ].  Note&lt;br /&gt;that the elements of the array is of the type TYPE.   array_name[ 0 ] can&lt;br /&gt;be anything, an int, char, struct etc.&lt;br /&gt;&lt;br /&gt;Diagrammatically, this can be viewed as:&lt;br /&gt;Figure-3:&lt;br /&gt;&lt;br /&gt;+——————————————+         +——————————+&lt;br /&gt;|  array_name  | ——————&gt; |   TYPE   | array_name[0]&lt;br /&gt;+——————————————+         +——————————+&lt;br /&gt;                        |   TYPE   | array_name[1]&lt;br /&gt;                        +——————————+&lt;br /&gt;                        |   TYPE   | array_name[2]&lt;br /&gt;                        +——————————+&lt;br /&gt;                        |   TYPE   | array_name[3]&lt;br /&gt;                        +——————————+&lt;br /&gt;                        |   TYPE   | array_name[4]&lt;br /&gt;                        +——————————+&lt;br /&gt;                        |   TYPE   | array_name[5]&lt;br /&gt;                        +——————————+&lt;br /&gt;                             .&lt;br /&gt;                             .&lt;br /&gt;                             .&lt;br /&gt;                             .&lt;br /&gt;                        +——————————+&lt;br /&gt;                        |   TYPE   | array_name[M-1]&lt;br /&gt;                        +——————————+&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;***************&lt;br /&gt;Variable names:&lt;br /&gt;***************&lt;br /&gt;&lt;br /&gt;When we declare an array as below:&lt;br /&gt;&lt;br /&gt;TYPE array_name[M];&lt;br /&gt;&lt;br /&gt;The compiler provides  the following variable names.&lt;br /&gt;&lt;br /&gt;array_name[0], array_name[1],  . . . . ,  array_name[M-1].&lt;br /&gt;&lt;br /&gt;These variable names can be manipulated  like any other variable. It should&lt;br /&gt;be noted that the M number of variables are of the type TYPE.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;*****************************&lt;br /&gt;Incrementing the array names:&lt;br /&gt;*****************************&lt;br /&gt;&lt;br /&gt;We saw earlier that in the case of single dimensional array, the array name&lt;br /&gt;(in our example this is array_name) is also a pointer to the first element,&lt;br /&gt;which in our examples is  array_name[0].&lt;br /&gt;&lt;br /&gt;We can't increment the contents of the array name, which is a constant, but&lt;br /&gt;we can do, for example&lt;br /&gt;&lt;br /&gt;p = array_name + 1;&lt;br /&gt;&lt;br /&gt;When we do this, p points to the next element, i.e.,  array_name[1].&lt;br /&gt;&lt;br /&gt;In general, if we do:&lt;br /&gt;&lt;br /&gt;p = (array_name + i)&lt;br /&gt;&lt;br /&gt;Then p points to the ith element, i.e.,   array_name[i]&lt;br /&gt;&lt;br /&gt;This result is important, which states that:&lt;br /&gt;&lt;br /&gt;( array_name + i )  points to element  array_name[i] .&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;******************************&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt;&lt;wbr&gt;******************************&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt;&lt;wbr&gt;*******&lt;br /&gt;Relationship between index and pointer in single dimensional array:&lt;br /&gt;******************************&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt;&lt;wbr&gt;******************************&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt;&lt;wbr&gt;*******&lt;br /&gt;&lt;br /&gt;We know that if p points to some object, then *p is that object&lt;br /&gt;Thus, it follows that- since&lt;br /&gt;&lt;br /&gt;(array_name + i ) points to array_name[i] ,&lt;br /&gt;&lt;br /&gt;therefore, it follows that&lt;br /&gt;&lt;br /&gt;*(array_name + i)  =   array_ name[i]&lt;br /&gt;&lt;br /&gt;the above relation, rewritten as below is the heart and soul of arrays and&lt;br /&gt;pointers&lt;br /&gt;&lt;br /&gt;array_name[i] =  *(array_name + i)&lt;br /&gt;&lt;br /&gt;An important corollary happens when we put i=0,  we have&lt;br /&gt;&lt;br /&gt;array_name[0] =  *array_name&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;******************************&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt;&lt;wbr&gt;********&lt;br /&gt;Alternative declaration of array name:&lt;br /&gt;******************************&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt;&lt;wbr&gt;********&lt;br /&gt;&lt;br /&gt;If we have a general single dimensional array declaration&lt;br /&gt;&lt;br /&gt;TYPE array_name[M];&lt;br /&gt;&lt;br /&gt;Then the array name can be considered to behave as the following&lt;br /&gt;declaration. Because array_name points to an element of type TYPE.&lt;br /&gt;&lt;br /&gt;TYPE *array_name;  /* i.e., array name points to TYPE */&lt;br /&gt;&lt;br /&gt;Thus, there are two ways we can write a prototype of a function in which&lt;br /&gt;the pointer array_name is passed.&lt;br /&gt;&lt;br /&gt;The first method is&lt;br /&gt;void f( TYPE  array_name[] );&lt;br /&gt;&lt;br /&gt;the second method is&lt;br /&gt;void f( TYPE  *array_name );&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;******************************&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt;&lt;wbr&gt;**********&lt;br /&gt;Pointers to arrays (single dimensional):&lt;br /&gt;******************************&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt;&lt;wbr&gt;**********&lt;br /&gt;&lt;br /&gt;A pointer to a single dimensional array is declared as, for example,&lt;br /&gt;&lt;br /&gt;char  (*p)[10] ;&lt;br /&gt;&lt;br /&gt;Here  p points to the  whole array of 10 characters, as shown in the&lt;br /&gt;diagram below&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;+——————————————+         +——————+&lt;br /&gt;|      p       | ——————&gt; | char |  0&lt;br /&gt;+——————————————+         +——————+&lt;br /&gt;                        | char |  1&lt;br /&gt;                        +——————+&lt;br /&gt;                        | char |  2&lt;br /&gt;                        +——————+&lt;br /&gt;                        | char |  3&lt;br /&gt;                        +——————+&lt;br /&gt;                        | char |  4&lt;br /&gt;                        +——————+&lt;br /&gt;                        | char |  5&lt;br /&gt;                        +——————+&lt;br /&gt;                        | char |  6&lt;br /&gt;                        +——————+&lt;br /&gt;                        | char |  7&lt;br /&gt;                        +——————+&lt;br /&gt;                        | char |  8&lt;br /&gt;                        +——————+&lt;br /&gt;                        | char |  9&lt;br /&gt;+——————————————+         +——————+&lt;br /&gt;|    p+1       | ——————&gt; | char |&lt;br /&gt;+——————————————+         +——————+&lt;br /&gt;                        | char |&lt;br /&gt;                        +——————+&lt;br /&gt;                            .&lt;br /&gt;                            .&lt;br /&gt;                            .&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Since  p points to an array of  10 chars therefore (*p) is the array of 10&lt;br /&gt;chars. That is, (*p) means the whole array of 10 characters. And just to&lt;br /&gt;confirm that-  we can print out sizeof(*p) we get 10 bytes.&lt;br /&gt;&lt;br /&gt;This also means, as per our earlier snippet, if we increment p by 1, the&lt;br /&gt;value will change by 10 bytes.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;***************&lt;br /&gt;The  Technique:&lt;br /&gt;***************&lt;br /&gt;Let us look at a few tricks for manipulating pointers to an array.&lt;br /&gt;&lt;br /&gt;Before coming to the tricks, The basics are that:&lt;br /&gt;any single dimensional array declaration has the following declaration&lt;br /&gt;&lt;br /&gt;TYPE     array_name[M] ;&lt;br /&gt;&lt;br /&gt;While pointer to an array type of declaration has the syntax.&lt;br /&gt;&lt;br /&gt;TYPE     ( *pointer_to_array )[M] ;&lt;br /&gt;&lt;br /&gt;If we compare the two we can see that , (*pointer_to_array) is nothing but&lt;br /&gt;the array name. What this means is that wherever results we had with&lt;br /&gt;respect to array_name previously, if we substitute it by&lt;br /&gt;(*pointer_to_array) we will get the same result. This is the basis of the&lt;br /&gt;trick. We will now apply this and see a few more methods.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We will use the following examples to show the methods.&lt;br /&gt;&lt;br /&gt;We will compare the following two declarations to explain the methods.&lt;br /&gt;&lt;br /&gt;char   (*p)[10] ;&lt;br /&gt;&lt;br /&gt;char    array_name[10];&lt;br /&gt;&lt;br /&gt;In the above case     (*p) is the name of the array&lt;br /&gt;&lt;br /&gt;Therefore:&lt;br /&gt;1.    Size of the array array_name is given by sizeof(array_name).  Since&lt;br /&gt;(*p) is the name of the array, therefore size of array (*p) is given by&lt;br /&gt;sizeof(*p).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;2.    The compiler creates the following memory variables array_name[0],&lt;br /&gt;array_name[1], array_name[ 2], .... ,  array_name[M-1] where they hold a&lt;br /&gt;char. Similarly, the compiler creates the following variable (*p)[0],&lt;br /&gt;(*p)[1], (*p)[2]  .... ,  (*p)[M-1], where each of the variable holds a&lt;br /&gt;char.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;3.    Just as array_name points to array_name[0], similarly, (*p) points to&lt;br /&gt;(*p)[0].&lt;br /&gt;&lt;br /&gt;If we perform (array_name + 1) it will point to array_name[1]. Similarly,&lt;br /&gt;if we do  (*p)+1 it will point to (*p)[1]. Which means that;&lt;br /&gt;&lt;br /&gt;(*p)[1] =  *((*p)+1)&lt;br /&gt;&lt;br /&gt; In general we have:&lt;br /&gt;&lt;br /&gt; (*p)[i] =  *((*p)+i)&lt;br /&gt;&lt;br /&gt;A corollary of this is:&lt;br /&gt;&lt;br /&gt;(*p)[0]  =  **p&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;4.    However the following analogy fails in the case of pointer to arrays.&lt;br /&gt;We said that, if there is a declaration as below;&lt;br /&gt;&lt;br /&gt;TYPE array_name[M];&lt;br /&gt;then, if we wish to  pass array_name as a parameter in the function, then&lt;br /&gt;there can be two ways of writing the prototype;&lt;br /&gt;&lt;br /&gt;void f( TYPE  array_name[] );&lt;br /&gt;&lt;br /&gt;or&lt;br /&gt;&lt;br /&gt;void f( TYPE  *array_name );&lt;br /&gt;&lt;br /&gt;The first method works for pointer to array, but the second method does not&lt;br /&gt;work.&lt;br /&gt;&lt;br /&gt;Thus, if we wish to pass a pointer to a single dimensional array in a&lt;br /&gt;function, then function prototype should be&lt;br /&gt;&lt;br /&gt;void f( TYPE  (*p)[] )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This will not work:&lt;br /&gt;void f( TYPE  **p)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Of course, in all the  above examples of pointer to arrays we have assumed&lt;br /&gt;that we have initialized ‘p’ correctly, may be by the pointer we get by a&lt;br /&gt;malloc or some other means.&lt;br /&gt;&lt;br /&gt;The following program will show more inputs about manipulating pointers to&lt;br /&gt;arrays,&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include&lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt; int arrray[10] = {0,1,2,3,4,5,6,7,8,9};&lt;br /&gt;&lt;br /&gt; int (*p)[10];&lt;br /&gt;&lt;br /&gt; int i;&lt;br /&gt;&lt;br /&gt; p = (int (*)[10]) arrray;     /* initialize p by casting array */&lt;br /&gt;&lt;br /&gt;  for(i=0;i&lt;10;i++)&lt;br /&gt;      printf("%d, %d, %d \n",arrray[i], (*p)[i], *((*p)+i));&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We will see the output as:&lt;br /&gt;&lt;br /&gt;0, 0, 0&lt;br /&gt;1, 1, 1&lt;br /&gt;2, 2, 2&lt;br /&gt;3, 3, 3&lt;br /&gt;4, 4, 4&lt;br /&gt;5, 5, 5&lt;br /&gt;6, 6, 6&lt;br /&gt;7, 7, 7&lt;br /&gt;8, 8, 8&lt;br /&gt;9, 9, 9&lt;br /&gt;&lt;br /&gt;which proves that  all the expressions in the printf statement are&lt;br /&gt;identical.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This little trick works in the following case too.&lt;br /&gt;&lt;br /&gt;Suppose we have&lt;br /&gt;&lt;br /&gt;int (**q)[10];&lt;br /&gt;If we have initialized q correctly then (**q) is the name of the array.&lt;br /&gt;Therefore&lt;br /&gt;&lt;br /&gt;1.  Size of array  is   sizeof(**q)&lt;br /&gt;&lt;br /&gt;2.  The following variables are created (**q)[0], (**q)[1],  ....,&lt;br /&gt;(**q)[9].&lt;br /&gt;&lt;br /&gt;3.  (**q) points to (**q)[0] and in general  (**q)[i]  =  *((**q)+i)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The following code will show  what we have meant so far.&lt;br /&gt;&lt;br /&gt;The data structure used in the following code is shown here:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;+——————————————+         +——————————————+         +——————+&lt;br /&gt;+——————————————+&lt;br /&gt;|      q       | ——————&gt; |       p      | ——————&gt; | int  |  &lt;—————— |&lt;br /&gt;arry      |&lt;br /&gt;+——————————————+         +——————————————+         +——————+&lt;br /&gt;+——————————————+&lt;br /&gt;                                                 | int  |  8&lt;br /&gt;                                                 +——————+&lt;br /&gt;                                                 | int  |  12&lt;br /&gt;                                                 +——————+&lt;br /&gt;                                                 | int  |  16&lt;br /&gt;                                                 +——————+&lt;br /&gt;                                                 | int  |  20&lt;br /&gt;                                                 +——————+&lt;br /&gt;                                                 | int  |  24&lt;br /&gt;                                                 +——————+&lt;br /&gt;                                                 | int  |  28&lt;br /&gt;                                                 +——————+&lt;br /&gt;                                                 | int  |  32&lt;br /&gt;                                                 +——————+&lt;br /&gt;                                                 | int  |  36&lt;br /&gt;                                                 +——————+&lt;br /&gt;                                                 | int  |  40  bytes&lt;br /&gt;                       +——————————————+          +——————+&lt;br /&gt;                       |    p+1       | ——————&gt;  | int  |&lt;br /&gt;                       +——————————————+          +——————+&lt;br /&gt;                                                 | int  |&lt;br /&gt;                                                 +——————+&lt;br /&gt;                                                     .&lt;br /&gt;                                                     .&lt;br /&gt;                                                     .&lt;br /&gt;&lt;br /&gt;sizeof(*p) = 40 bytes,  assuming 32-bit integer.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include&lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt; int arry[10] = {0,1,2,3,4,5,6,7,8,9};&lt;br /&gt;&lt;br /&gt; int (*p)[10];&lt;br /&gt;&lt;br /&gt; int (**q)[10];&lt;br /&gt;&lt;br /&gt; int i;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; p = (int(*)[10])arry;   /* initialize p with pointer array */&lt;br /&gt;&lt;br /&gt; q = &amp;p;      /* initialize q with the address of p */&lt;br /&gt;&lt;br /&gt;  for(i=0;i&lt;10;i++)&lt;br /&gt;      printf("%d, %d, %d, %d, %d, %d \n",&lt;br /&gt;        arry[i], *(arry+i), (*p)[i], *((*p)+i), (**q)[i], *((**q)+i));&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;==============================&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt;&lt;wbr&gt;========&lt;br /&gt;Result:&lt;br /&gt;0, 0, 0, 0, 0, 0&lt;br /&gt;1, 1, 1, 1, 1, 1&lt;br /&gt;2, 2, 2, 2, 2, 2&lt;br /&gt;3, 3, 3, 3, 3, 3&lt;br /&gt;4, 4, 4, 4, 4, 4&lt;br /&gt;5, 5, 5, 5, 5, 5&lt;br /&gt;6, 6, 6, 6, 6, 6&lt;br /&gt;7, 7, 7, 7, 7, 7&lt;br /&gt;8, 8, 8, 8, 8, 8&lt;br /&gt;9, 9, 9, 9, 9, 9&lt;br /&gt;&lt;br /&gt; As we see all the expressions in the printf statement have the same&lt;br /&gt;meaning.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the next snippet, we will discuss about pointers to two-dimensional&lt;br /&gt;arrays.&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1075957451022844053-2922917116512945557?l=c-unix-linux.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-unix-linux.blogspot.com/feeds/2922917116512945557/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1075957451022844053&amp;postID=2922917116512945557' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/2922917116512945557'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/2922917116512945557'/><link rel='alternate' type='text/html' href='http://c-unix-linux.blogspot.com/2009/05/arrays-and-pointer-to-arrays-one.html' title='Arrays and Pointer to arrays : One Dimension'/><author><name>Sarsij Nayanam</name><uri>https://profiles.google.com/108798285946939476460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-Et8dADNkSFA/AAAAAAAAAAI/AAAAAAAAB-k/_Ygfe7sXRto/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1075957451022844053.post-2354217612832020139</id><published>2009-05-11T08:18:00.000-07:00</published><updated>2009-05-11T08:23:14.737-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C snippet'/><category scheme='http://www.blogger.com/atom/ns#' term='C Tutorial'/><title type='text'>Preventing Precedence Errors: EqualitytInequality</title><content type='html'>&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="border-collapse: collapse;  font-size:13px;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;A very commonly observed precedence error during usage of expressions is inadequate parenthesizing.  Here we take examples of Equality/Inequality expressions:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span"   style="border-collapse: collapse;   font-family:arial;font-size:13px;"&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;if(var1 &amp;amp; var2 == 0)&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;The above is incorrect because == has a higher precedence than &amp;amp;, therefore in the above expression  var2==0  will be computed first, which is a bool. Then that bool will be bit_AND'ed with var1.  And this was not our intention.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;The correct way  is:&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;if((var1 &amp;amp; var2) == 0)&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;In practice, the test for equality/inequality must be the last logical operation, but it can happen otherwise, if there are operators of lower precedence than  ==  or   !=  in the expression.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;If we recall our three liner:&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;S—U—Ari&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;Re—Bit—Lo—C&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;Assignment—Comma&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;The  “e”  in Re stands for equality/inequality, and when they are there in an expression with operators of lower precedence then the  ==  or !=  will get executed earlier. To change the order of evaluation we need to appropriately parenthesize these operators. These operators(with precedence lower than  ==  and   != )  are   Bit—Lo—C—Assignment—Comma.   In practice we will find bit and assignment operator mixed with  ==  and   != operators.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;So when we see this mixture, i.e.,==  or  !=, &amp;amp;,  | ,  ^, =   in a statement,  parenthesis are to be taken care.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;The following are some examples where precedence errors are very common.&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;a)    Test if FLAG_A is clear in message incorrect&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;if(message &amp;amp; FLAG_A == 0)&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;correct&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;if((message &amp;amp; FLAG_A) == 0)&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;b)    Test if FLAG_ A is set in message  incorrect&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;if(message &amp;amp; FLAG_A != 0)&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;correct&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;if((message &amp;amp; FLAG_A) != 0)&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;c)    Test if all flags are clear incorrect&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;if ( message &amp;amp; (FLAG_A | FLAG_B | FLAG_C) == 0)&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt; correct&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;if (( message &amp;amp; (FLAG_A | FLAG_B | FLAG_C)) ==0)&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;d)    Incorrect&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;if ( c = getchar() != EOF)&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;correct&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;if (( c = getchar()) != EOF)&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;In all the above cases, parenthesizing is to be done for operands with precedence lower than  == or !=  so that they are executed earlier.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;In short, when and expression has   equality/inequality operator as the final operand and we see that there are operators in the expression whose precedence levels and are still lower than equality / inequality, then we should get the parenthesis correct. The operators with lower precedence than  “Re” are  Bit—Lo—C—Assignment—Comma&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1075957451022844053-2354217612832020139?l=c-unix-linux.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-unix-linux.blogspot.com/feeds/2354217612832020139/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1075957451022844053&amp;postID=2354217612832020139' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/2354217612832020139'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/2354217612832020139'/><link rel='alternate' type='text/html' href='http://c-unix-linux.blogspot.com/2009/05/snippetpreventing-precedence-errors.html' title='Preventing Precedence Errors: EqualitytInequality'/><author><name>Sarsij Nayanam</name><uri>https://profiles.google.com/108798285946939476460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-Et8dADNkSFA/AAAAAAAAAAI/AAAAAAAAB-k/_Ygfe7sXRto/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1075957451022844053.post-6348946343619318567</id><published>2009-04-16T13:19:00.000-07:00</published><updated>2009-04-16T13:22:29.999-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C snippet'/><category scheme='http://www.blogger.com/atom/ns#' term='sizeof'/><category scheme='http://www.blogger.com/atom/ns#' term='C Tutorial'/><title type='text'>How to measure the size of a variable</title><content type='html'>&lt;span class="Apple-style-span" style="border-collapse: collapse; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="font-family: 'trebuchet ms';"&gt;Let us go through a small test to find out if this entire &lt;/span&gt;&lt;span class="il" style="background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(255, 255, 204); background-position: initial initial; "&gt;&lt;span class="Apple-style-span" style="font-family: 'trebuchet ms';"&gt;snippet&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'trebuchet ms';"&gt; is really worth reading. The test begins now.  We should complete it in 60 seconds!&lt;br /&gt;&lt;br /&gt;Consider these  declarations in a code.&lt;br /&gt;&lt;br /&gt;     char x, y, *p, **pp;&lt;br /&gt;     char  *array[5], arr[5];&lt;br /&gt;     char  *f(int), **g(int) ;&lt;br /&gt;&lt;br /&gt;   struct node  a, b,* h(int); / * assume the size of node is 20 */&lt;br /&gt;&lt;br /&gt;Then, what will be the values of the following?&lt;br /&gt;&lt;br /&gt;1.    sizeof(x)&lt;br /&gt;2.    sizeof(y)&lt;br /&gt;3.    sizeof(*p)&lt;br /&gt;4.    sizeof(p)&lt;br /&gt;5.    sizeof(**pp)&lt;br /&gt;6.    sizeof(*pp)&lt;br /&gt;7.    sizeof(pp)&lt;br /&gt;8.    sizeof(* array [0])&lt;br /&gt;9.    sizeof(array[0])&lt;br /&gt;10.   sizeof(arr [5] )&lt;br /&gt;11.   sizeof(*f(5))&lt;br /&gt;12.   sizeof(f(5))    /* is the size of a function?! */&lt;br /&gt;13.   sizeof(**g(6))&lt;br /&gt;14.   sizeof(*g(6))&lt;br /&gt;15.   sizeof(g(6))&lt;br /&gt;16.   sizeof(a)&lt;br /&gt;17.   sizeof(*b)&lt;br /&gt;18.   sizeof(b)&lt;br /&gt;19.   sizeof(*h(5))&lt;br /&gt;20.   sizeof(h(5))&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Considering that size of char is 1 byte and pointers have a size of 4 bytes. Here  are the answers.&lt;br /&gt;&lt;br /&gt;1.    1 byte&lt;br /&gt;2.    1 byte&lt;br /&gt;3.    1 byte&lt;br /&gt;4.    4 bytes&lt;br /&gt;5.    1 byte&lt;br /&gt;6.    4 bytes&lt;br /&gt;7.    4 bytes&lt;br /&gt;8.    1 byte&lt;br /&gt;9.    4 bytes&lt;br /&gt;10.   1 byte&lt;br /&gt;11.   1byte&lt;br /&gt;12.   4 bytes&lt;br /&gt;13.   1 byte&lt;br /&gt;14.   4 bytes&lt;br /&gt;15.   4 bytes&lt;br /&gt;16.   20 bytes&lt;br /&gt;17.   20 bytes&lt;br /&gt;18.   4 bytes&lt;br /&gt;19.   20 bytes&lt;br /&gt;20.   4 bytes&lt;br /&gt;&lt;br /&gt;We are almost sure; we got all the answers right, but did we beat the clock? Did we do it within 60 seconds?&lt;br /&gt;&lt;br /&gt;Well! Here is how to get the measure on size really fast… For this we need to remember the following two things. &lt;/span&gt;&lt;/span&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="border-collapse: collapse; font-family: 'trebuchet ms'; font-size: 13px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="border-collapse: collapse; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="font-family: 'trebuchet ms';"&gt;First: refresh what we discussed in the “Moving the Stars” &lt;/span&gt;&lt;span class="il" style="background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(255, 255, 204); background-position: initial initial; "&gt;&lt;span class="Apple-style-span" style="font-family: 'trebuchet ms';"&gt;snippet&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'trebuchet ms';"&gt;. That virtual equal to sign helps a lot in getting the size right. Second: Any pointer of any type has the size 4 bytes (we are by default considering 32 bit machines. The pointer size can, of course, change with machines.)&lt;br /&gt;&lt;br /&gt;Let us answer the questions we put earlier…&lt;br /&gt;&lt;br /&gt;Question 1 and Answer:&lt;br /&gt;     We have&lt;br /&gt;      char x;&lt;br /&gt;&lt;br /&gt;     Which can be read as&lt;br /&gt;      char = x&lt;br /&gt;&lt;br /&gt;     Therefore,&lt;br /&gt;      sizeof(x) = sizeof(char) = 1&lt;br /&gt;&lt;br /&gt;Question 2 and Answer:&lt;br /&gt;     As above, the answer is again 1 byte&lt;br /&gt;&lt;br /&gt;Question 3 and Answer:&lt;br /&gt;     Given&lt;br /&gt;      char *p;&lt;br /&gt;     What is the sizeof(*p)?&lt;br /&gt;&lt;br /&gt;     Because char = *p, therefore&lt;br /&gt;      sizeof(*p)= sizeof(char) = 1&lt;br /&gt;&lt;br /&gt;Question 4 and Answer:&lt;br /&gt;     Given&lt;br /&gt;      char  *p;&lt;br /&gt;     What is the sizeof(p)?&lt;br /&gt;&lt;br /&gt;     Because char *  = p, therefore,&lt;br /&gt;      sizeof(p) = sizeof(char *) = 4&lt;br /&gt;&lt;br /&gt;Question 5 and Answer:&lt;br /&gt;     Given&lt;br /&gt;      char **pp;&lt;br /&gt;     What is the sizeof (**pp)?&lt;br /&gt;&lt;br /&gt;     Because char = **pp, therefore&lt;br /&gt;      sizeof (**pp) = sizeof (char) = 1&lt;br /&gt;&lt;br /&gt;Question 6 and Answer:&lt;br /&gt;     Given,&lt;br /&gt;      char **pp;&lt;br /&gt;      What is the  sizeof(*pp)?&lt;br /&gt;&lt;br /&gt;     Because char * =*pp, therefore&lt;br /&gt;      sizeof(*pp) = sizeof(char *) = 4&lt;br /&gt;&lt;br /&gt;Question 7 and Answer:&lt;br /&gt;     Given,&lt;br /&gt;      char **pp,&lt;br /&gt;     What is the sizeof (pp)?&lt;br /&gt;&lt;br /&gt;     Because char ** = p, therefore&lt;br /&gt;      sizeof(p) = sizeof(char **) = 4&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Question 8 is interesting, because we are apparently trying to find the size of a function, which is unusual. But we can compute the size of functions. However what it means is the size of its return values.&lt;br /&gt;&lt;br /&gt;Let us take up Question 8 and Answer:&lt;br /&gt;&lt;br /&gt;     Given char * f(int); what is sizeof(*f(5));&lt;br /&gt;&lt;br /&gt;     Because char = * f(int), therefore&lt;br /&gt;      sizeof (* f(5)) = sizeof(char) = 1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Question 9 and Answer:&lt;br /&gt;&lt;br /&gt;     Given  char *f(int) what is  sizeof(f(5))?&lt;br /&gt;&lt;br /&gt;     Because  char * = f(int), therefore       sizeof (f(5)) = sizeof(char *) = 4.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now that we know what we mean by size of a function, the remaining questions should be easy to answer quickly. Let's do it to be sure. This trick works with any C/C++ declarations.&lt;br /&gt;&lt;br /&gt;By the way, why do we need to know the size of any variable correctly, the answer is to malloc correctly to get the correct amount of memory. If we give a wrong size for memory allocation, chances are that we will take days&lt;br /&gt;to find out why the program misbehaves in strange ways.&lt;br /&gt;&lt;br /&gt;This &lt;/span&gt;&lt;span class="il" style="background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(255, 255, 204); background-position: initial initial; "&gt;&lt;span class="Apple-style-span" style="font-family: 'trebuchet ms';"&gt;snippet&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'trebuchet ms';"&gt; has helped to explain in detail the aspects of sizeof.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1075957451022844053-6348946343619318567?l=c-unix-linux.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-unix-linux.blogspot.com/feeds/6348946343619318567/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1075957451022844053&amp;postID=6348946343619318567' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/6348946343619318567'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/6348946343619318567'/><link rel='alternate' type='text/html' href='http://c-unix-linux.blogspot.com/2009/04/how-to-measure-size-of-variable.html' title='How to measure the size of a variable'/><author><name>Sarsij Nayanam</name><uri>https://profiles.google.com/108798285946939476460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-Et8dADNkSFA/AAAAAAAAAAI/AAAAAAAAB-k/_Ygfe7sXRto/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1075957451022844053.post-7613621725249564732</id><published>2009-04-04T11:31:00.000-07:00</published><updated>2009-04-04T11:35:53.439-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C snippet'/><category scheme='http://www.blogger.com/atom/ns#' term='C Tutorial'/><category scheme='http://www.blogger.com/atom/ns#' term='Pointers'/><title type='text'>Refresh your Pointers knowledge</title><content type='html'>&lt;span class="Apple-style-span" style="border-collapse: collapse; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="font-family: 'trebuchet ms';"&gt;This little piece of literature will let us prevent a category of defects related to pointers. But first, let us take a small test. It will help us to decide if the entire &lt;/span&gt;&lt;span class="il" style="background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: rgb(255, 255, 204); background-position: initial initial; "&gt;&lt;span class="Apple-style-span" style="font-family: 'trebuchet ms';"&gt;snippet&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'trebuchet ms';"&gt;  is really worth reading. So here are the questions.&lt;br /&gt;&lt;br /&gt;Consider, following are some of the declarations in a piece of code.&lt;br /&gt;    &lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt; int x , y, *p, **pp ;&lt;br /&gt;   int *array[5], arr[5];&lt;br /&gt;   int *f(int), **g(int);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now, for which of the following eight statements the compiler will complain and for which it will not complain?&lt;br /&gt;Let us try the test; check if we can hit it right in less than sixty seconds!&lt;br /&gt;1.    x = y;&lt;br /&gt;2.    *p = array [0];&lt;br /&gt;3.    *p = **pp;&lt;br /&gt;4.    x = f (4);&lt;br /&gt;5.    p = *g (3);&lt;br /&gt;6.    array [0] = arr[0];&lt;br /&gt;7.    arr [0] = * array [1];&lt;br /&gt;8.    array [5] = **g (3);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here are the answers. For all odd number statements 1,3,5… .. the compiler will not complain.&lt;br /&gt;For all even number statements 2,4,6… . The compiler will complain.&lt;br /&gt;&lt;br /&gt;Let us now figure out quickly some of the mistakes we saw earlier. The way to find  what works and what does not, is rather simple. That is because, we can consider a C/C++ declarations as an equation with an “=” sign, across which you can move the stars!&lt;/span&gt;&lt;/span&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="border-collapse: collapse; font-size: 13px; "&gt;&lt;span class="Apple-style-span" style="font-family: 'trebuchet ms';"&gt;&lt;br /&gt;Let's hold on! We will see what we meant above&lt;br /&gt;&lt;br /&gt;Let us consider the declaration  int * x;&lt;br /&gt;&lt;br /&gt;Let's imagine an "equal to" sign in between as follows&lt;br /&gt;&lt;br /&gt;     int = * x ; /* this one tells you that *x is int */&lt;br /&gt;&lt;br /&gt;We can move the star across the “=” sign, as shown here:&lt;br /&gt;&lt;br /&gt;     int * = x ; /* this one tells that x is an integer pointer or a&lt;br /&gt;pointer to an integer*/&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;One more example will make things clearer.&lt;br /&gt;&lt;br /&gt;     int ** y&lt;br /&gt;&lt;br /&gt;Let's imagine it as:&lt;br /&gt;&lt;br /&gt;     int = **y   (this means  **y is an integer).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;After moving one star across:&lt;br /&gt;&lt;br /&gt;     int * = *y ( this means that *y is an integer pointer).&lt;br /&gt;&lt;br /&gt;After moving one more star across:&lt;br /&gt;     int ** = y,  this means that y is an integer pointer pointer.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now , let us combine the above two examples&lt;br /&gt;      In the first example we saw  *x  is an integer&lt;br /&gt;     In the second example we saw that  **y  is also an integer.&lt;br /&gt;&lt;br /&gt;So, the compiler will not complain, if we write&lt;br /&gt;     *x = **y&lt;br /&gt;          or&lt;br /&gt;     **y = *x&lt;br /&gt;&lt;br /&gt;Similarly,&lt;br /&gt;     x = * y   is also ok with the compiler because both the sides are&lt;br /&gt;int *.&lt;br /&gt;&lt;br /&gt;By now we have seen, how to quickly find what will work and what will not work by  MOVING THE STARS!&lt;br /&gt;It is a good idea to have a look at the declarations for minimizing related defects.&lt;br /&gt;&lt;br /&gt;Let us go back to the quiz questions we discussed earlier. We will go through the answers quickly by using this  MOVING THE STARS! method.&lt;br /&gt;&lt;br /&gt;We have copied and pasted the declarations over here for easy reference:&lt;br /&gt;     int x , y, *p, **pp ;&lt;br /&gt;     int *array[5], arr[5];&lt;br /&gt;     int *f(int), **g(int);&lt;br /&gt;&lt;br /&gt;1.    x = y  is correct, because both sides are int&lt;br /&gt;2.    *p = array [0] is wrong, because LHS is int but RHS is  int *&lt;br /&gt;3.    *p = **pp    is correct because both sides are int.&lt;br /&gt;4.    x = f (4) is wrong because LHS is  int and RHS is int *.&lt;br /&gt;5.    p= *g (3) is correct because both sides are int *&lt;br /&gt;6.    array [0] = arr [0] is wrong because LHS is int *  but RHS is  int.&lt;br /&gt;7.    arr [0] = * array [1]   is correct, because both sides are  int.&lt;br /&gt;8.    array [5] = ** g (3) is wrong, because LHS is  int*  while RHS is int.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Experience shows that regularly using this little trick helps in writing as well as reading code faster.&lt;br /&gt;Of course, it also helps us in reducing mistakes.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1075957451022844053-7613621725249564732?l=c-unix-linux.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-unix-linux.blogspot.com/feeds/7613621725249564732/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1075957451022844053&amp;postID=7613621725249564732' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/7613621725249564732'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/7613621725249564732'/><link rel='alternate' type='text/html' href='http://c-unix-linux.blogspot.com/2009/04/refresh-your-pointers-knowledge.html' title='Refresh your Pointers knowledge'/><author><name>Sarsij Nayanam</name><uri>https://profiles.google.com/108798285946939476460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-Et8dADNkSFA/AAAAAAAAAAI/AAAAAAAAB-k/_Ygfe7sXRto/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1075957451022844053.post-6977139229338655099</id><published>2009-02-13T10:53:00.000-08:00</published><updated>2009-02-13T11:08:10.682-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='goto'/><title type='text'>goto</title><content type='html'>- A goto statement causes your program to unconditionally transfer control to the statement associated with the label specified on the goto staement. &lt;br /&gt;&lt;br /&gt;- Because the goto statement can interfere with the normal sequence of processing, it makes a program more difficult and maintain. Often, a break statement, a continue statement, or a function call can eliminate the need for a goto statement. &lt;br /&gt;&lt;br /&gt;- If an active block is exited using the goto statement, any local variables are destroyed when the control is transferred from that block. &lt;br /&gt;&lt;br /&gt;- Also , one cannot use a goto statement to jump over initializations. &lt;br /&gt;&lt;br /&gt;- A goto statement is allowed to jump within the scope of a variable length array, but not past any declaration of objects with variably modified types. &lt;br /&gt;&lt;code&gt;&lt;br /&gt;goto name1; &lt;br /&gt;/* name1 is the identifier for the jumping location */&lt;br /&gt;/* name1 is a valid variable followed by colon */&lt;br /&gt;&lt;br /&gt;…………. …………. &lt;br /&gt;…………. …………. &lt;br /&gt;…………. …………. &lt;br /&gt;name1:  Statement;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Eversince I started writing C codes (which is more than a decade back) I have been instructed not to use the keyword 'goto' ever. and I were to say that we all have been doing a goto-less programming then it would not be out of place. Infact, goto is much like a contradiction to the philosophy of C. C follows a structured instruction flow, and use of goto makes it break that flow and starts jumping the code sequence. &lt;br /&gt;&lt;br /&gt;The inclusion of goto in the ANSI C itself shows that it has some utility in the programming, and should be used whenever and wherever required. The most common use of goto statements is when you need to jump out of multiple loops for a safe landing, and it is expected that it should not be used for jumping out of single loops. Instead, whenever we encounter multiple loops, say 3 or 4, we need to use goto. &lt;br /&gt;&lt;br /&gt;Having said that goto is the most efficient code line when we need to jump out of multiple loops, it is also a must to know that the landing code should not be with-in a loop (or a loop with-in a loop). &lt;br /&gt;&lt;br /&gt;An example of such a code using goto is here : &lt;br /&gt;&lt;br /&gt;&lt;code&gt;#include &lt;stdio.h&gt;&lt;br /&gt;void display(int matrix[3][3]);&lt;br /&gt; &lt;br /&gt;int main(void)&lt;br /&gt;{&lt;br /&gt;   int matrix[3][3]=    {1,2,3,4,5,2,8,9,10};&lt;br /&gt;   display(matrix);&lt;br /&gt;   return(0);&lt;br /&gt;}&lt;br /&gt; &lt;br /&gt;void display(int matrix[3][3])&lt;br /&gt;{&lt;br /&gt;   int i, j;&lt;br /&gt; &lt;br /&gt;   for (i = 0; i &lt; 3; i++)&lt;br /&gt;      for (j = 0; j &lt; 3; j++)&lt;br /&gt;      {&lt;br /&gt;         if ( (matrix[i][j] &lt; 1) || (matrix[i][j] &gt; 6) )&lt;br /&gt;            goto out_of_bounds;&lt;br /&gt;         printf("matrix[%d][%d] = %d\n", i, j, matrix[i][j]);&lt;br /&gt;      }&lt;br /&gt;   return;&lt;br /&gt;   out_of_bounds: printf("number must be 1 through 6\n");&lt;br /&gt;}&lt;/code&gt; &lt;br /&gt;&lt;br /&gt;And finally here are some interesting facts against goto statements.......&lt;br /&gt;&lt;br /&gt;1. MATLAB does not have goto at all, and it sometimes make programming so much difficult without it.&lt;br /&gt;&lt;br /&gt;2. Java has a reserved keyword as goto, but does not define its syntax. &lt;br /&gt;&lt;br /&gt;3. Dijkstra's letter against goto : http://www.cs.ubbcluj.ro/~adriana/FP/Requirements/dijkstra68goto.pdf&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1075957451022844053-6977139229338655099?l=c-unix-linux.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-unix-linux.blogspot.com/feeds/6977139229338655099/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1075957451022844053&amp;postID=6977139229338655099' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/6977139229338655099'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/6977139229338655099'/><link rel='alternate' type='text/html' href='http://c-unix-linux.blogspot.com/2009/02/goto.html' title='goto'/><author><name>Sarsij Nayanam</name><uri>https://profiles.google.com/108798285946939476460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-Et8dADNkSFA/AAAAAAAAAAI/AAAAAAAAB-k/_Ygfe7sXRto/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1075957451022844053.post-8941711229618002927</id><published>2009-01-29T07:35:00.000-08:00</published><updated>2009-01-29T07:36:56.234-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='csh'/><title type='text'>csh programming should be avoided</title><content type='html'>&lt;span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: helvetica; font-size: 12px; line-height: 18px; "&gt;I am continually shocked and dismayed to see people write test cases, install scripts, and other random hackery using the csh. Lack of proficiency in the Bourne shell has been known to cause errors in /etc/rc and .cronrc files, which is a problem, because you MUST write these files in that language.&lt;br /&gt;&lt;br /&gt;The csh is attractive because the conditionals are more C-like, so the path of least resistance is chosen and a csh script is written. Sadly, this is a lost cause, and the programmer seldom even realizes it, even when they find that many simple things they wish to do range from cumbersome to impossible in the csh.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;1. FILE DESCRIPTORS&lt;br /&gt;&lt;br /&gt;The most common problem encountered in csh programming is that you can't do file-descriptor manipulation. All you are able to do is redirect stdin, or stdout, or dup stderr into stdout. &lt;br /&gt;Bourne-compatible shells offer you an abundance of more exotic possibilities. &lt;br /&gt;&lt;br /&gt;1a. Writing Files&lt;br /&gt;&lt;br /&gt;In the Bourne shell, you can open or dup arbitrary file descriptors.&lt;br /&gt;For example, &lt;br /&gt;&lt;br /&gt;exec 2&gt;errs.out&lt;br /&gt;&lt;br /&gt;means that from then on, stderr goes into errs file.&lt;br /&gt;&lt;br /&gt;Or what if you just want to throw away stderr and leave stdout&lt;br /&gt;alone? Pretty simple operation, eh?&lt;br /&gt;&lt;br /&gt;cmd 2&gt;/dev/null&lt;br /&gt;&lt;br /&gt;Works in the Bourne shell. In the csh, you can only make a pitiful &lt;br /&gt;attempt like this:&lt;br /&gt;&lt;br /&gt;(cmd &gt; /dev/tty) &gt;&amp;amp; /dev/null&lt;br /&gt;&lt;br /&gt;But who said that stdout was my tty? So it's wrong. This simple&lt;br /&gt;operation *CANNOT BE DONE* in the csh.&lt;br /&gt;&lt;br /&gt;Along these same lines, you can't direct error messages in csh scripts&lt;br /&gt;out stderr as is considered proper. In the Bourne shell, you might say:&lt;br /&gt;&lt;br /&gt;echo "$0: cannot find $file" 1&gt;&amp;amp;2&lt;br /&gt;&lt;br /&gt;but in the csh, you can't redirect stdout out stderr, so you end&lt;br /&gt;up doing something silly like this:&lt;br /&gt;&lt;br /&gt;sh -c 'echo "$0: cannot find $file" 1&gt;&amp;amp;2'&lt;br /&gt;&lt;br /&gt;1b. Reading Files&lt;br /&gt;&lt;br /&gt;In the csh, all you've got is $&lt;, which reads a line from your tty. What&lt;br /&gt;if you've redirected stdin? Tough noogies, you still get your tty, which &lt;br /&gt;you really can't redirect. Now, the read statement &lt;br /&gt;in the Bourne shell allows you to read from stdin, which catches&lt;br /&gt;redirection. It also means that you can do things like this:&lt;br /&gt;&lt;br /&gt;exec 3 &lt; file1&lt;br /&gt;exec 4 &lt; file2&lt;br /&gt;&lt;br /&gt;Now you can read from fd 3 and get lines from file1, or from file2 through&lt;br /&gt;fd 4. In modern, Bourne-like shells, this suffices: &lt;br /&gt;&lt;br /&gt;read some_var 0&lt;&amp;amp;3&lt;br /&gt;read another_var 0&lt;&amp;amp;4&lt;br /&gt;&lt;br /&gt;Although in older ones where read only goes from 0, you trick it:&lt;br /&gt;&lt;br /&gt;exec 5&lt;&amp;amp;0 # save old stdin&lt;br /&gt;exec 0&lt;&amp;3; read some_var&lt;br /&gt;exec 0&lt;&amp;4; read another_var&lt;br /&gt;exec 0&lt;&amp;amp;5 # restore it&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;1c. Closing FDs&lt;br /&gt;&lt;br /&gt;In the Bourne shell, you can close file descriptors you don't&lt;br /&gt;want open, like 2&gt;&amp;amp;-, which isn't the same as redirecting it&lt;br /&gt;to /dev/null.&lt;br /&gt;&lt;br /&gt;1d. More Elaborate Combinations&lt;br /&gt;&lt;br /&gt;Maybe you want to pipe stderr to a command and leave stdout alone.&lt;br /&gt;Not too hard an idea, right? You can't do this in the csh as I&lt;br /&gt;mentioned in 1a. In a Bourne shell, you can do things like this:&lt;br /&gt;&lt;br /&gt;exec 3&gt;&amp;1; grep yyy xxx 2&gt;&amp;amp;1 1&gt;&amp;amp;3 3&gt;&amp;amp;- | sed s/file/foobar/ 1&gt;&amp;amp;2 3&gt;&amp;amp;-&lt;br /&gt;grep: xxx: No such foobar or directory&lt;br /&gt;&lt;br /&gt;Normal output would be unaffected. The closes there were in case&lt;br /&gt;something really cared about all its FDs. We send stderr to sed,&lt;br /&gt;and then put it back out 2.&lt;br /&gt;&lt;br /&gt;Consider the pipeline:&lt;br /&gt;&lt;br /&gt;A | B | C&lt;br /&gt;&lt;br /&gt;You want to know the status of C, well, that's easy: it's in $?, or&lt;br /&gt;$status in csh. But if you want it from A, you're out of luck -- if&lt;br /&gt;you're in the csh, that is. In the Bourne shell, you can get it, although&lt;br /&gt;doing so is a bit tricky. Here's something I had to do where I ran dd's&lt;br /&gt;stderr into a grep -v pipe to get rid of the records in/out noise, but had&lt;br /&gt;to return the dd's exit status, not the grep's:&lt;br /&gt;&lt;br /&gt;device=/dev/rmt8&lt;br /&gt;dd_noise='^[0-9]+\+[0-9]+ records (in|out)$'&lt;br /&gt;exec 3&gt;&amp;amp;1&lt;br /&gt;status=`((dd if=$device ibs=64k 2&gt;&amp;amp;1 1&gt;&amp;amp;3 3&gt;&amp;amp;- 4&gt;&amp;amp;-; echo $? &gt;&amp;amp;4) |&lt;br /&gt;egrep -v "$dd_noise" 1&gt;&amp;amp;2 3&gt;&amp;amp;- 4&gt;&amp;amp;-) 4&gt;&amp;amp;1`&lt;br /&gt;exit $status;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The csh has also been known to close all open file descriptors besides&lt;br /&gt;the ones it knows about, making it unsuitable for applications that &lt;br /&gt;intend to inherit open file descriptors.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;2. COMMAND ORTHOGONALITY&lt;br /&gt;&lt;br /&gt;2a. Built-ins&lt;br /&gt;&lt;br /&gt;The csh is a horrid botch with its built-ins. You can't put them&lt;br /&gt;together in many reasonable ways. Even simple little things like this:&lt;br /&gt;&lt;br /&gt;% time | echo&lt;br /&gt;&lt;br /&gt;which while nonsensical, shouldn't give me this message:&lt;br /&gt;&lt;br /&gt;Reset tty pgrp from 9341 to 26678&lt;br /&gt;&lt;br /&gt;Others are more fun:&lt;br /&gt;% sleep 1 | while&lt;br /&gt;while: Too few arguments.&lt;br /&gt;[5] 9402&lt;br /&gt;% jobs&lt;br /&gt;[5] 9402 Done sleep |&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Some can even hang your shell. Try typing ^Z while you're sourcing &lt;br /&gt;something, or redirecting a source command. Just make sure you have&lt;br /&gt;another window handy. Or try &lt;br /&gt;&lt;br /&gt;% history | more&lt;br /&gt;&lt;br /&gt;on some systems.&lt;br /&gt;Aliases are not evaluated everywhere you would like them do be:&lt;br /&gt;&lt;br /&gt;% alias lu 'ls -u'&lt;br /&gt;% lu&lt;br /&gt;HISTORY News bin fortran lib lyrics misc tex&lt;br /&gt;Mail TEX dehnung hpview logs mbox netlib&lt;br /&gt;% repeat 3 lu&lt;br /&gt;lu: Command not found.&lt;br /&gt;lu: Command not found.&lt;br /&gt;lu: Command not found.&lt;br /&gt;&lt;br /&gt;% time lu&lt;br /&gt;lu: Command not found.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;2b. Flow control&lt;br /&gt;&lt;br /&gt;You can't mix flow-control and commands, like this:&lt;br /&gt;&lt;br /&gt;who | while read line; do&lt;br /&gt;echo "gotta $line"&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;You can't combine multiline constructs in a csh using semicolons.&lt;br /&gt;There's no easy way to do this&lt;br /&gt;&lt;br /&gt;alias cmd 'if (foo) then bar; else snark; endif'&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;You can't perform redirections with if statements that are&lt;br /&gt;evaluated solely for their exit status:&lt;br /&gt;&lt;br /&gt;if ( { grep vt100 /etc/termcap &gt; /dev/null } ) echo ok&lt;br /&gt;&lt;br /&gt;And even pipes don't work:&lt;br /&gt;&lt;br /&gt;if ( { grep vt100 /etc/termcap | sed 's/$/###' } ) echo ok&lt;br /&gt;&lt;br /&gt;But these work just fine in the Bourne shell:&lt;br /&gt;if grep vt100 /etc/termcap &gt; /dev/null ; then echo ok; fi &lt;br /&gt;if grep vt100 /etc/termcap | sed 's/$/###/' ; then echo ok; fi&lt;br /&gt;&lt;br /&gt;Consider the following reasonable construct:&lt;br /&gt;&lt;br /&gt;if ( { command1 | command2 } ) then&lt;br /&gt;...&lt;br /&gt;endif&lt;br /&gt;&lt;br /&gt;The output of command1 won't go into the input of command2. You will get&lt;br /&gt;the output of both commands on standard output. No error is raised. In&lt;br /&gt;the Bourne shell or its clones, you would say &lt;br /&gt;&lt;br /&gt;if command1 | command2 ; then&lt;br /&gt;...&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;2c. Stupid parsing bugs&lt;br /&gt;&lt;br /&gt;Certain reasonable things just don't work, like this:&lt;br /&gt;&lt;br /&gt;% kill -1 `cat foo`&lt;br /&gt;`cat foo`: Ambiguous.&lt;br /&gt;&lt;br /&gt;But this is ok:&lt;br /&gt;&lt;br /&gt;% /bin/kill -1 `cat foo`&lt;br /&gt;&lt;br /&gt;If you have a stopped job:&lt;br /&gt;&lt;br /&gt;[2] Stopped rlogin globhost&lt;br /&gt;&lt;br /&gt;You should be able to kill it with &lt;br /&gt;&lt;br /&gt;% kill %?glob&lt;br /&gt;kill: No match&lt;br /&gt;&lt;br /&gt;but&lt;br /&gt;&lt;br /&gt;% fg %?glob&lt;br /&gt;&lt;br /&gt;works.&lt;br /&gt;&lt;br /&gt;White space can matter:&lt;br /&gt;&lt;br /&gt;if(expr)&lt;br /&gt;&lt;br /&gt;may fail on some versions of csh, while&lt;br /&gt;&lt;br /&gt;if (expr)&lt;br /&gt;&lt;br /&gt;works! Your vendor may have attempted to fix this bug, but odds are good&lt;br /&gt;that their csh still won't be able to handle&lt;br /&gt;&lt;br /&gt;if(0) then&lt;br /&gt;if(1) then&lt;br /&gt;echo A: got here&lt;br /&gt;else&lt;br /&gt;echo B: got here&lt;br /&gt;endif&lt;br /&gt;echo We should never execute this statement&lt;br /&gt;endif&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;3. SIGNALS&lt;br /&gt;&lt;br /&gt;In the csh, all you can do with signals is trap SIGINT. In the Bourne&lt;br /&gt;shell, you can trap any signal, or the end-of-program exit. For example,&lt;br /&gt;to blow away a tempfile on any of a variety of signals:&lt;br /&gt;&lt;br /&gt;$ trap 'rm -f /usr/adm/tmp/i$$ ;&lt;br /&gt;echo "ERROR: abnormal exit";&lt;br /&gt;exit' 1 2 3 15&lt;br /&gt;&lt;br /&gt;$ trap 'rm tmp.$$' 0 # on program exit&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;4. QUOTING&lt;br /&gt;You can't quote things reasonably in the csh:&lt;br /&gt;&lt;br /&gt;set foo = "Bill asked, \"How's tricks?\""&lt;br /&gt;&lt;br /&gt;doesn't work. This makes it really hard to construct strings with&lt;br /&gt;mixed quotes in them. In the Bourne shell, this works just fine. &lt;br /&gt;In fact, so does this:&lt;br /&gt;&lt;br /&gt;cd /mnt; /usr/ucb/finger -m -s `ls \`u\``&lt;br /&gt;&lt;br /&gt;Dollar signs cannot be escaped in double quotes in the csh. Ug.&lt;br /&gt;&lt;br /&gt;set foo = "this is a \$dollar quoted and this is $HOME not quoted" &lt;br /&gt;dollar: Undefined variable.&lt;br /&gt;&lt;br /&gt;You have to use backslashes for newlines, and it's just darn hard to&lt;br /&gt;get them into strings sometimes.&lt;br /&gt;&lt;br /&gt;set foo = "this \&lt;br /&gt;and that";&lt;br /&gt;echo $foo&lt;br /&gt;this and that&lt;br /&gt;echo "$foo"&lt;br /&gt;Unmatched ". &lt;br /&gt;&lt;br /&gt;Say what? You don't have these problems in the Bourne shell, where it's&lt;br /&gt;just fine to write things like this:&lt;br /&gt;&lt;br /&gt;echo 'This is &lt;br /&gt;some text that contains&lt;br /&gt;several newlines.'&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;As distributed, quoting history references is a challenge. Consider:&lt;br /&gt;&lt;br /&gt;% mail adec23!alberta!pixel.Convex.COM!tchrist&lt;br /&gt;alberta!pixel.Convex.COM!tchri: Event not found.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;5. VARIABLE SYNTAX&lt;br /&gt;&lt;br /&gt;There's this big difference between global (environment) and local&lt;br /&gt;(shell) variables. In csh, you use a totally different syntax &lt;br /&gt;to set one from the other. &lt;br /&gt;&lt;br /&gt;In the Bourne shell, this &lt;br /&gt;VAR=foo cmds args&lt;br /&gt;is the same as&lt;br /&gt;(export VAR; VAR=foo; cmd args)&lt;br /&gt;or csh's&lt;br /&gt;(setenv VAR; cmd args)&lt;br /&gt;&lt;br /&gt;You can't use :t, :h, etc on envariables. Watch:&lt;br /&gt;echo Try testing with $SHELL:t&lt;br /&gt;&lt;br /&gt;It's really nice to be able to say&lt;br /&gt;&lt;br /&gt;${PAGER-more}&lt;br /&gt;or&lt;br /&gt;FOO=${BAR:-${BAZ}}&lt;br /&gt;&lt;br /&gt;to be able to run the user's PAGER if set, and more otherwise.&lt;br /&gt;You can't do this in the csh. It takes more verbiage.&lt;br /&gt;&lt;br /&gt;You can't get the process number of the last background command from the&lt;br /&gt;csh, something you might like to do if you're starting up several jobs in&lt;br /&gt;the background. In the Bourne shell, the pid of the last command put in&lt;br /&gt;the background is available in $!.&lt;br /&gt;&lt;br /&gt;The csh is also flaky about what it does when it imports an &lt;br /&gt;environment variable into a local shell variable, as it does&lt;br /&gt;with HOME, USER, PATH, and TERM. Consider this:&lt;br /&gt;&lt;br /&gt;% setenv TERM '`/bin/ls -l / &gt; /dev/tty`'&lt;br /&gt;% csh -f&lt;br /&gt;&lt;br /&gt;And watch the fun!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;6. EXPRESSION EVALUATION&lt;br /&gt;&lt;br /&gt;Consider this statement in the csh:&lt;br /&gt;if ($?MANPAGER) setenv PAGER $MANPAGER&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Despite your attempts to only set PAGER when you want&lt;br /&gt;to, the csh aborts:&lt;br /&gt;&lt;br /&gt;MANPAGER: Undefined variable.&lt;br /&gt;&lt;br /&gt;That's because it parses the whole line anyway AND EVALUATES IT!&lt;br /&gt;You have to write this:&lt;br /&gt;&lt;br /&gt;if ($?MANPAGER) then&lt;br /&gt;setenv PAGER $MANPAGER&lt;br /&gt;endif&lt;br /&gt;&lt;br /&gt;That's the same problem you have here:&lt;br /&gt;&lt;br /&gt;if ($?X &amp;amp;&amp;amp; $X == 'foo') echo ok&lt;br /&gt;X: Undefined variable&lt;br /&gt;&lt;br /&gt;This forces you to write a couple nested if statements. This is highly&lt;br /&gt;undesirable because it renders short-circuit booleans useless in&lt;br /&gt;situations like these. If the csh were the really C-like, you would&lt;br /&gt;expect to be able to safely employ this kind of logic. Consider the&lt;br /&gt;common C construct:&lt;br /&gt;&lt;br /&gt;if (p &amp;amp;&amp;amp; p-&gt;member) &lt;br /&gt;&lt;br /&gt;Undefined variables are not fatal errors in the Bourne shell, so &lt;br /&gt;this issue does not arise there.&lt;br /&gt;&lt;br /&gt;While the csh does have built-in expression handling, it's not&lt;br /&gt;what you might think. In fact, it's space sensitive. This is an&lt;br /&gt;error&lt;br /&gt;&lt;br /&gt;@ a = 4/2&lt;br /&gt;&lt;br /&gt;but this is ok&lt;br /&gt;&lt;br /&gt;@ a = 4 / 2&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The ad hoc parsing csh employs fouls you up in other places &lt;br /&gt;as well. Consider:&lt;br /&gt;&lt;br /&gt;% alias foo 'echo hi' ; foo&lt;br /&gt;foo: Command not found.&lt;br /&gt;% foo&lt;br /&gt;hi&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;7. ERROR HANDLING&lt;br /&gt;&lt;br /&gt;Wouldn't it be nice to know you had an error in your script before&lt;br /&gt;you ran it? That's what the -n flag is for: just check the syntax.&lt;br /&gt;This is especially good to make sure seldom taken segments of code&lt;br /&gt;code are correct. Alas, the csh implementation of this doesn't work.&lt;br /&gt;Consider this statement:&lt;br /&gt;&lt;br /&gt;exit (i)&lt;br /&gt;&lt;br /&gt;Of course, they really meant&lt;br /&gt;&lt;br /&gt;exit (1)&lt;br /&gt;&lt;br /&gt;or just&lt;br /&gt;&lt;br /&gt;exit 1&lt;br /&gt;&lt;br /&gt;Either shell will complain about this. But if you hide this in an if&lt;br /&gt;clause, like so:&lt;br /&gt;&lt;br /&gt;#!/bin/csh -fn&lt;br /&gt;if (1) then&lt;br /&gt;exit (i)&lt;br /&gt;endif&lt;br /&gt;&lt;br /&gt;The csh tells you there's nothing wrong with this script. The equivalent&lt;br /&gt;construct in the Bourne shell, on the other hand, tells you this:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#!/bin/sh -n&lt;br /&gt;if (1) then&lt;br /&gt;exit (i)&lt;br /&gt;endif&lt;br /&gt;&lt;br /&gt;/tmp/x: syntax error at line 3: `(' unexpected&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;RANDOM BUGS&lt;br /&gt;&lt;br /&gt;Here's one:&lt;br /&gt;&lt;br /&gt;fg %?string&lt;br /&gt;^Z&lt;br /&gt;&lt;span style="font-family: 'courier new'; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "&gt;&lt;/span&gt;kill %?string&lt;br /&gt;No match.&lt;br /&gt;&lt;br /&gt;Huh? Here's another&lt;br /&gt;&lt;br /&gt;!%s%x%s&lt;br /&gt;&lt;br /&gt;Coredump, or garbage.&lt;br /&gt;&lt;br /&gt;If you have an alias with backquotes, and use that in backquotes in &lt;br /&gt;another one, you get a coredump.&lt;br /&gt;&lt;br /&gt;Try this:&lt;br /&gt;% repeat 3 echo "/vmu*"&lt;br /&gt;/vmu*&lt;br /&gt;/vmunix&lt;br /&gt;/vmunix&lt;br /&gt;What???&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here's another one:&lt;br /&gt;&lt;br /&gt;% mkdir tst&lt;br /&gt;% cd tst&lt;br /&gt;% touch '[foo]bar'&lt;br /&gt;% foreach var ( * )&lt;br /&gt;&gt; echo "File named $var"&lt;br /&gt;&gt; end&lt;br /&gt;foreach: No match.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;8. SUMMARY&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;While some vendors have fixed some of the csh's bugs (the tcsh also does much better here), many have added new ones. Most of its problems can never be solved because they're not actually bugs, but rather the direct consequences of ill design decisions. It's inherently flawed.&lt;br /&gt;&lt;br /&gt;Do yourself a favor, and if you *have* to write a shell script, do it in the Bourne shell. It's on every UNIX system out there. However, behavior can vary.&lt;br /&gt;&lt;br /&gt;There are other possibilities.&lt;br /&gt;&lt;br /&gt;The Korn shell is the preferred programming shell by many sh addicts, but it still suffers from inherent problems in the Bourne shell's design,such as parsing and evaluation horrors. The Korn shell or its public-domain clones and supersets (like bash) aren't quite so ubiquitous&lt;br /&gt;as sh, so it probably wouldn't be wise to write a sharchive in them that you post to the net. When 1003.2 becomes a real standard that companies are forced to adhere to, then we'll be in much better shape. Until then, we'll be stuck with bug-incompatible versions of the sh lying about.&lt;br /&gt;&lt;br /&gt;If you have a problem that would ordinarily use sed or awk or sh, but it exceeds their capabilities or must run a little faster, and you don't want to write the silly thing in C, then Perl may be for you. You can get at networking functions, binary data, and most of the C library. There are also translators to turn your sed and awk scripts into Perl scripts,&lt;br /&gt;as well as a symbolic debugger. Tchrist's personal rule of thumb is that if it's the size that fits in a Makefile, it gets written in the Bourne shell, but anything bigger gets written in Perl.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1075957451022844053-8941711229618002927?l=c-unix-linux.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-unix-linux.blogspot.com/feeds/8941711229618002927/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1075957451022844053&amp;postID=8941711229618002927' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/8941711229618002927'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/8941711229618002927'/><link rel='alternate' type='text/html' href='http://c-unix-linux.blogspot.com/2009/01/csh-programming-should-be-avoided.html' title='csh programming should be avoided'/><author><name>Sarsij Nayanam</name><uri>https://profiles.google.com/108798285946939476460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-Et8dADNkSFA/AAAAAAAAAAI/AAAAAAAAB-k/_Ygfe7sXRto/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1075957451022844053.post-6079754059350696688</id><published>2009-01-03T22:10:00.000-08:00</published><updated>2009-01-03T22:47:39.116-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ebook'/><category scheme='http://www.blogger.com/atom/ns#' term='Let Us C'/><title type='text'>Let Us C by Yaswant Kanitkar</title><content type='html'>Anyone who is looking for a softcopy of 'Let Us C' by Yashwant Kanitkar can drop me a request in the comments section of this page. I will send the book by email (so also put your email ID while putting the request).&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Hope you will have a great time reading this book, as it has become a universal passport to all the C related questions at the freshers level.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Keep coming back.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;bbye!&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1075957451022844053-6079754059350696688?l=c-unix-linux.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-unix-linux.blogspot.com/feeds/6079754059350696688/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1075957451022844053&amp;postID=6079754059350696688' title='21 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/6079754059350696688'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/6079754059350696688'/><link rel='alternate' type='text/html' href='http://c-unix-linux.blogspot.com/2009/01/let-us-c-by-yaswant-kanitkar.html' title='Let Us C by Yaswant Kanitkar'/><author><name>Sarsij Nayanam</name><uri>https://profiles.google.com/108798285946939476460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-Et8dADNkSFA/AAAAAAAAAAI/AAAAAAAAB-k/_Ygfe7sXRto/s512-c/photo.jpg'/></author><thr:total>21</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1075957451022844053.post-6724375707669375199</id><published>2009-01-01T02:40:00.000-08:00</published><updated>2009-01-01T03:28:41.125-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Ubuntu'/><title type='text'>Ubuntu 8.10 'Hardy Heron'  released!</title><content type='html'>&lt;span style="font-size:85%;"&gt;&lt;span style="font-family: trebuchet ms;"&gt;Ubuntu 8.10 'Hardy Heron' version has been launched with a Beta tag, and is ready for download from its &lt;a href="http://www.ubuntu.com/download"&gt;website&lt;/a&gt;.  &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The main driving point for Ubuntu has been the Windows user, and the way it has been designed and created is with an intention to make the user shift away from Windows to Ubuntu. Even in the last 8.04 edition it has paid special attention to the ease with which a user can install Ubuntu on his Windows PC. And this feature remains to be there in this 8.10 edition too.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:85%;"&gt;Hardy Heron beta incorporates GNOME 2.22 which brings with it a whole raft of new features, changes, fixes and improvements. One of the most significant changes is the new Nautilus file manager that uses the GVFS virtual File system.&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;In this version Firefox 3.0 Beta 4 has been provided as the default browser.&lt;br /&gt;&lt;br /&gt;CD and DVD burning just got a lot easier with Brasero. It's wonderfully quick and easy to use.&lt;br /&gt;&lt;br /&gt;The upgraded kernel (now 2.6.24-12.13), which brings with it power management for 64-bit users, kernel-based virtualization and the 'Completely Fair Scheduler' process.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: trebuchet ms;"&gt;New Features in Ubuntu 8.10&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: trebuchet ms;"&gt;-------------------------------------------------&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;h3 style="font-family: trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;3G Support&lt;/span&gt;&lt;/h3&gt; &lt;p style="font-family: trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt; For constant connectivity public WiFi has limitations. Improvements to the network manager in Ubuntu 8.10 makes it simple to detect and connect to 3G networks and manage connectivity. This connectivity is delivered through an inbuilt 3G modem, through 'dongle' support, through a mobile phone or through Bluetooth. It is a complex environment that Ubuntu 8.10 simplifies through a single interface and the auto-detection of many of the most popular devices. &lt;/span&gt;&lt;/p&gt; &lt;h3 style="font-family: trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;Write Ubuntu to and Install from a USB Drive&lt;/span&gt;&lt;/h3&gt; &lt;p style="font-family: trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt; Ubuntu has been made available to users as an image for CDs and DVDs to date. But CDs and DVDs are slower, less portable and less convenient than USB sticks. Now, a simple application in Ubuntu will allow users to write Ubuntu to a USB drive, even a modified version of Ubuntu with their data on it, so it can be carried everywhere to plug in and use on any machine. &lt;/span&gt;&lt;/p&gt; &lt;h3 style="font-family: trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;Guest Sessions&lt;/span&gt;&lt;/h3&gt; &lt;p style="font-family: trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt; In a world of 'always on' pervasive computing it is more likely that users lend their computers to colleagues or friends at conferences, cafes or at parties so they can check email, etc. Guest sessions allow users to lock down a session easily so a guest can use the full system without interference with programs or data. &lt;/span&gt;&lt;/p&gt; &lt;h3 style="font-family: trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;BBC Content&lt;/span&gt;&lt;/h3&gt; &lt;p style="font-family: trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt; Starting the media players within Ubuntu (Totem Movie Player and Rhythmbox) launches a menu of selected content from the broadcaster that is free to air. This is a mixture of video, radio and podcasts and available in high quality, much of it playable using non-proprietary codecs. Content is constantly updated via the corporation's stream and will vary dependent on location, though some content will be available for every user. &lt;/span&gt;&lt;/p&gt; &lt;h3 style="font-family: trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;Latest Gnome 2.24 Desktop Environment &lt;/span&gt;&lt;/h3&gt; &lt;p style="font-family: trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt; The GNOME desktop environment project releases its latest version which is incorporated into Ubuntu 8.10. New features include a new instant messaging client, a built-in time tracker, improved file management and toolbars plus better support for multiple monitor use with the ability to set screen resolution by monitor.&lt;/span&gt;&lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;Finally, with this release of Ubuntu it becomes almost sure that more and more number of users will start migrating to it. This happens to be the best of all the available Unix flavours for personal systems like Desktops and the Notepads.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1075957451022844053-6724375707669375199?l=c-unix-linux.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-unix-linux.blogspot.com/feeds/6724375707669375199/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1075957451022844053&amp;postID=6724375707669375199' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/6724375707669375199'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/6724375707669375199'/><link rel='alternate' type='text/html' href='http://c-unix-linux.blogspot.com/2009/01/ubuntu-810-hardy-heron-released.html' title='Ubuntu 8.10 &apos;Hardy Heron&apos;  released!'/><author><name>Sarsij Nayanam</name><uri>https://profiles.google.com/108798285946939476460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-Et8dADNkSFA/AAAAAAAAAAI/AAAAAAAAB-k/_Ygfe7sXRto/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1075957451022844053.post-4599640819720566782</id><published>2009-01-01T01:29:00.000-08:00</published><updated>2009-01-02T20:27:49.835-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='memset'/><category scheme='http://www.blogger.com/atom/ns#' term='bzero'/><title type='text'>bzero() and memset()</title><content type='html'>&lt;div  style="color: rgb(0, 0, 0); font-family: courier new;font-family:&amp;quot;;"&gt;&lt;span style="font-size:85%;"&gt;&lt;b&gt;bzero&lt;/b&gt;() and &lt;b&gt;memset&lt;/b&gt;() are two functions used for similar use - to set or initialize the values of a memory segment to zero (or NULL). Where bzero() is used only for setting the values to zero, memset() is also used for initializing the values to some others values too.&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: courier new; color: rgb(0, 0, 0);"&gt;&lt;/div&gt;&lt;div  style="color: rgb(0, 0, 0); font-family: courier new;font-family:&amp;quot;;"&gt;&lt;span style="font-size:85%;"&gt;the function prototype for both are like as below:&lt;/span&gt;&lt;/div&gt;&lt;div  style="text-align: justify; color: rgb(0, 0, 0); font-family: courier new;font-family:courier new;"&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;pre  style="color: rgb(0, 0, 0); font-family: courier new;font-family:courier new;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style=""&gt;#include &lt;string.h&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre  style="color: rgb(0, 0, 0); font-family: courier new;font-family:courier new;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style=""&gt;&lt;string.h&gt;void &lt;b&gt;bzero&lt;/b&gt;(void *mem, int bytes);&lt;/string.h&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre  style="color: rgb(0, 0, 0); font-family: courier new;font-family:courier new;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style=""&gt;&lt;string.h&gt;void* &lt;b&gt;memset&lt;/b&gt;(void *mem, int val, int bytes);&lt;/string.h&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre  style="color: rgb(0, 0, 0); font-family: courier new;font-family:courier new;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style=""&gt; &lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre  style="color: rgb(0, 0, 0); font-family: courier new;font-family:courier new;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style=""&gt;mem : the memory segment to initialize, holds the address of the starting block.&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre  style="color: rgb(0, 0, 0); font-family: courier new;font-family:courier new;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style=""&gt;bytes : Size of the memory segment.&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre  style="color: rgb(0, 0, 0); font-family: courier new;font-family:courier new;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style=""&gt;val :  value to be filled in the segment.&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre  style="color: rgb(0, 0, 0); font-family: courier new;font-family:courier new;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style=""&gt; &lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre  style="color: rgb(0, 0, 0); font-family: courier new;font-family:courier new;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style=""&gt;(both these functions are defined in the string.h header file, and hence it needs to be included&lt;br /&gt;whenever these are included in the file.)&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre  style="color: rgb(0, 0, 0); font-family: courier new;font-family:courier new;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style=""&gt; &lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre  style="color: rgb(0, 0, 0); font-family: courier new;font-family:courier new;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style=""&gt; &lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre face="courier new" style="color: rgb(0, 0, 0); font-family: courier new;"&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-size: 13px; "&gt;These two functions have been historically been used in an interchangable manner, the reason being the&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style="font-size:85%;"&gt;&lt;div style="text-align: justify;"&gt;nature of UNIX and its vastness. bzero() was originally a part of the BSD-Unix, and memset() was a part of&lt;br /&gt;&lt;/div&gt;&lt;span style=""&gt;&lt;div style="text-align: justify;"&gt;the AT&amp;amp;T Unix. And later memset() was also adopted in the ANSI C and POSIX standards whereas bzero()&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;was depracated. And while writing professional codes it is not advisable to use bzero() and longer, instead&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;memset() should be used. (Although both will remain to be supported in the all the platforms).&lt;br /&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre  style="text-align: justify;color: rgb(0, 0, 0); font-family: 'courier new'; font-family:courier new;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style=""&gt; &lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1075957451022844053-4599640819720566782?l=c-unix-linux.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-unix-linux.blogspot.com/feeds/4599640819720566782/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1075957451022844053&amp;postID=4599640819720566782' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/4599640819720566782'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/4599640819720566782'/><link rel='alternate' type='text/html' href='http://c-unix-linux.blogspot.com/2009/01/bzero-and-memset.html' title='bzero() and memset()'/><author><name>Sarsij Nayanam</name><uri>https://profiles.google.com/108798285946939476460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-Et8dADNkSFA/AAAAAAAAAAI/AAAAAAAAB-k/_Ygfe7sXRto/s512-c/photo.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1075957451022844053.post-955907552614386566</id><published>2008-11-25T06:10:00.000-08:00</published><updated>2008-11-25T09:08:20.982-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Ubuntu'/><title type='text'>Ubuntu</title><content type='html'>Last night I installed Ubuntu after a lot of procrastination for weeks. And I am currently posting this blog using Ubuntu OS. (for that matter, I have decided that I will post all the articles on this page using only the Unix machines).&lt;br /&gt;&lt;br /&gt;Till now it has been a great fun working on this OS, and hope things continue to remain the same way (Microsoft guys, are you listening). What's even more interesting to know is that Ubuntu is absolutely free of cost, and if you have the patience to wait for a few weeks than these guys will ship you a free CD of Ubuntu, or else you can download it from their web-site. (&lt;a href="http://www.ubuntu.com/"&gt;www.ubuntu.com&lt;/a&gt;)&lt;br /&gt;&lt;br /&gt;Although I am in no position to claim that this is the best flavour of all the Linux versions, but till now I have found it much smoother and much faster than the Windows Vista performance on the same system.&lt;br /&gt;&lt;br /&gt;Hope, at least some of you would give Ubuntu a chance to grow and survive this OS war&lt;br /&gt;&lt;br /&gt;Praise for Ubuntu :&lt;br /&gt;&lt;a href="http://education.zdnet.com/?p=1969&amp;amp;tag=nl.e539"&gt;Are you sure you don't want to you use Ubuntu?&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1075957451022844053-955907552614386566?l=c-unix-linux.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-unix-linux.blogspot.com/feeds/955907552614386566/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1075957451022844053&amp;postID=955907552614386566' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/955907552614386566'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/955907552614386566'/><link rel='alternate' type='text/html' href='http://c-unix-linux.blogspot.com/2008/11/ubuntu.html' title='Ubuntu'/><author><name>Sarsij Nayanam</name><uri>https://profiles.google.com/108798285946939476460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-Et8dADNkSFA/AAAAAAAAAAI/AAAAAAAAB-k/_Ygfe7sXRto/s512-c/photo.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1075957451022844053.post-2228276975604908914</id><published>2008-11-25T05:58:00.001-08:00</published><updated>2008-11-25T06:10:15.058-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Hello World'/><title type='text'>Hello world</title><content type='html'>Long long ago, someone wrote "Hello World" program using the C language, no one had ever expected that the tradition of saying hello to whole world would catch up so fast, and so religiously that even after decades of programming and sophisticated computing systems available, the first thing anyone would want to do with that system will be to say - Hello world.&lt;br /&gt;&lt;br /&gt;Keeping in pace with the same tradition, I also felt like saying hello to this whole world of computer enthusiasts and programmers/students in specific.&lt;br /&gt;&lt;br /&gt;Well, to introduce myself - I am actually a no-one in this huge world of software, and when I began working as a software engineer I had a self doubt that why would anyone ever pay me for writing codes. But things have changes since then, and actually someone is paying me writing the same (although, I still remain to be no-one in this software world).&lt;br /&gt;&lt;br /&gt;My work has offered me enough opportunity to explore the possibilities with C programming and its affiliates - Unix and Linux. I am not a guru of these three close associates, but then I all I say is that if you are looking for some excitement in the field of programming using these three tools, then I can assure you that I could be of some help to you all. Especially for students, I guess if nothing more then I can still help you get the edge over the others.&lt;br /&gt;&lt;br /&gt;I am open to answer all your questions and answers related to C, Unix, and Linux but I can't assure that I will be able to answer those questions with-in a given time-limit. So, in case, there is no hurry for you to get the answers then please feel free to drop your questions to me at my personal email ID (sarsij.nayanam@gmail.com) and in the comments section of this page.&lt;br /&gt;&lt;br /&gt;Hope we will enjoy the journey with C-Unix-Linux.&lt;br /&gt;&lt;br /&gt;Thanks &amp;amp; Regards&lt;br /&gt;&lt;br /&gt;Sarsij Nayanam&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1075957451022844053-2228276975604908914?l=c-unix-linux.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://c-unix-linux.blogspot.com/feeds/2228276975604908914/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1075957451022844053&amp;postID=2228276975604908914' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/2228276975604908914'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1075957451022844053/posts/default/2228276975604908914'/><link rel='alternate' type='text/html' href='http://c-unix-linux.blogspot.com/2008/11/hello-world.html' title='Hello world'/><author><name>Sarsij Nayanam</name><uri>https://profiles.google.com/108798285946939476460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-Et8dADNkSFA/AAAAAAAAAAI/AAAAAAAAB-k/_Ygfe7sXRto/s512-c/photo.jpg'/></author><thr:total>0</thr:total></entry></feed>
