Latest News Updates

Latest News    

Monday, 5 December 2011

JMS 2 Demo

C Pointers Fundamentals Explained with Examples – Part I


Anybody who is working on Linux environment (not just developers), should understand the fundamentals of C programming language and write some basic C program.
This article is part of our ongoing series on C programming language.
The concept of pointers is one of the most powerful fundamentals of C/C++ language.

Through pointers a developer can directly access memory from his/her code which makes memory related operations very fast. But, as always, with great power comes great responsibility.
A developer has to very carefully make use of pointers in order to avoid some problems that can be nightmare to debug.
In this article we will study the very basic concept of pointers with examples in C language.

What are Pointers?

Different from other normal variables which can store values, pointers are special variables that can hold the address of a variable. Since they store memory address of a variable, the pointers are very commonly said to “point to variables”. Lets try to understand the concept.

As shown in the above diagram:
  • A normal variable ‘var’ has a memory address of 1001 and holds a value 50.
  • A pointer variable has its own address 2047 but stores 1001, which is the address of the variable ‘var’

How to Declare a Pointer?

A pointer is declared as :
<pointer type> *<pointer-name>
In the above declaration :
  1. pointer-type : It specifies the type of pointer. It can be int,char, float etc. This type specifies the type of variable whose address this pointer can store.
  2. pointer-name : It can be any name specified by the user. Professionally, there are some coding styles which every code follows. The pointer names commonly start with ‘p’ or end with ‘ptr’
An example of a pointer declaration can be :
char *chptr;
In the above declaration, ‘char’ signifies the pointer type, chptr is the name of the pointer while the asterisk ‘*’ signifies that ‘chptr’ is a pointer variable.

How to initialize a Pointer?

A pointer is initialized in the following way :
<pointer declaration(except semicolon)> = <address of a variable>

OR 

<pointer declaration>
<name-of-pointer> = <address of a variable>
Note that the type of variable above should be same as the pointer type.(Though this is not a strict rule but for beginners this should be kept in mind).
For example :
char ch = 'c';
char *chptr = &ch; //initialize 

OR 

char ch = 'c';
char *chptr;
chptr = &ch //initialize
In the code above, we declared a character variable ch which stores the value ‘c’. Now, we declared a character pointer ‘chptr’ and initialized it with the address of variable ‘ch’.
Note that the ‘&’ operator is used to access the address of any type of variable.

How to Use a Pointer?

A pointer can be used in two contexts.
Context 1: For accessing the address of the variable whose memory address the pointer stores.
Again consider the following code :
   char ch = 'c';
   char *chptr = &ch;
Now, whenever we refer the name ‘chptr’ in the code after the above two lines, then compiler would try to fetch the value contained by this pointer variable, which is the address of the variable (ch) to which the pointer points. i.e. the value given by ‘chptr’ would be equal to ‘&ch’.
For example :
char *ptr = chptr;
The value held by ‘chptr’ (which in this case is the address of the variable ‘ch’) is assigned to the new pointer ‘ptr’.
Context 2: For accessing the value of the variable whose memory address the pointer stores.
Continuing with the piece of code used above :
char ch = 'c';
char t;
char *chptr = &ch;
t = *chptr;
We see that in the last line above, we have used ‘*’ before the name of the pointer. What does this asterisk operator do?
Well, this operator when applied to a pointer variable name(like in the last line above) yields the value of the variable to which this pointer points. Which means, in this case ‘*chptr’ would yield the value kept at address held by chptr. Since ‘chptr’ holds the address of variable ‘ch’ and value of ‘ch’ is ‘c’, so ‘*chptr’ yeilds ‘c’.
When used with pointers, the asterisk ‘*’ operator is also known as ‘value of’ operator.

An Example of C Pointers

Consider the following code :
CODE :
#include <stdio.h> 

int main(void)
{
    char ch = 'c';
    char *chptr = &ch; 

    int i = 20;
    int *intptr = &i; 

    float f = 1.20000;
    float *fptr = &f; 

    char *ptr = "I am a string"; 

    printf("\n [%c], [%d], [%f], [%c], [%s]\n", *chptr, *intptr, *fptr, *ptr, ptr);

    return 0;
}
OUTPUT :
$ ./pointers 

 [c], [20], [1.200000], [I], [I am a string]
To debug a C program, use gdb. The above code covers all the common pointers. The first three of them are very trivial now to understand so lets concentrate on the fourth one. In the fourth example, a character pointer points to a string.
In C, a string is nothing but an array of characters. So we have no staring pointers in C. Its the character pointers that are used in case of strings too.
Now, coming to the string, when we point a pointer to a string, by default it holds the address of the first character of the string. Lets try to understand it better.
The string, ‘I am String’ in memory is placed as :
1001   1002    1003    1004   1005   1006   1007   1008   1009   1010
I      a       m       S      t      r      i      n      g      \0
Since characters occupy one byte each, so they are placed like above in the memory. Note the last character, its a null character which is placed at the end of every string by default in C. This null character signifies the end of the string.
Now coming back to the point, any character pointer pointing to a string stores the address of the first character of the string. In the code above, ‘ptr’ holds the address of the character ‘I’ ie 1001. Now, when we apply the ‘value of’ operator ‘*’ to ‘ptr’, we intend to fetch the value at address 1001 which is ‘I’ and hence when we print ‘*ptr’, we get ‘I’ as the output.
Also, If we specify the format specifier as ‘%s’ and use ‘ptr’ (which contains the starting address of the string), then the complete string is printed using printf. The concept is that %s specifier requires the address of the beginning byte of string to display the complete string, which we provided using ‘ptr’ (which we know holds the beginning byte address of the string). This we can see as the last print in the output above.

Pointers as Structure Objects

Consider the following code :
CODE:
#include<stdio.h> 

struct st{
int a;
char ch;
}; 

int main(void)
{
    struct st obj;
    struct st *stobj = &obj; 

    stobj->a = 5;
    stobj->ch = 'a'; 

    printf("\n [%d] [%c]\n", stobj->a, stobj->ch);
    return 0;
}
OUTPUT:
$ ./pointers 

 [5] [a]
In the above code, we have declared a pointer stobj of type ‘struct st’. Now since the pointer type is a structure, so the address it points to has to be of a ‘struct st’ type variable(which in this case is ‘obj’). Other interesting part is how structure elements are accessed using pointer variable ‘stobj’. Yes, When dealing with pointer objects, its a standard to use arrow operator -> instead of ‘.’ operator(which would have been used, had we used ‘obj’ to access the structure elements).
To conclude, In this article we studied the concept of pointers in C from scratch and then slowly built upon our understanding to more complex topics like using pointers as structure objects. This was a basic tutorial, we will cover more complex pointer concepts in the part-II of this article.

sourcec: The geekstuff

Java JMS full

Long Term Infrastructure Bonds 2011 by IDFC, REC, IIFCL, IFCI, L and T, ICICI, LIC and SBI

Govt. of India has allowed certain Infrastructure companies to issue Long Term Tax Saving Infrastructure Bonds. These companies include IDFC, IFCI, REC, IIFCL, L and T, ICICI etc. Investors of these Infra Bonds can claim tax exemption under section 80 CCF of the Income Tax Act, 1961 for an amount up to Rs. 20000. This deduction is aver and above Rs. 100000 limit available under sections 80C, 80CCC and 80CCD read with section 80CCE. Most of the companies have issued notifications for the issue of bonds with interest rates ranging from 7.75 percent to 8.30 percent. There are various options for investors such as buy back by the company after 5 / 7 years and maturity option after 10 / 15 years under different series of Bonds.

These Infra bonds provide tax benefits apart from creating additional value by the time of maturity. Though the companies are not promising any assured returns, one can expect minimum returns on par with any saving schemes as the the sector will get highest priority by the Governments. These Infra Bonds are most effective in the case of high salaried people with 30 percent tax bracket. Following are these details of Long Term Infra Bonds for the year 2011-12

1. REC Long Term Infra Bonds: Issue Last date: Till 31st March 2011; Interest Rate: 8 percent for buyback option and 8.10 percent for Non-Buy back option. Website: recindia.nic.in

2. IIFCL Infra Bonds: Issue Closes on 4th March 2011. Interest: 8.15 for buy back and 8.30 for till the maturity option. Website: www.iifcl.org

3. IDFC Infrastructure Bonds: Issued series I and Series II with an intereste rate of 8 percent for both Buy back and cumulative options. Website: www.idfc.com

4. IFCI Long Term Infra Bonds: The IFCI has issued Series I and Series II Bonds with an interest rates of 8 percent for Buy Back option and 8.25 for Non Buy Back option. Website: www.ifciltd.com

5. L and T Infra Bonds: Issued Long Term Tax Saver Infra Bonds with 7.5 percent interest rate for buy back after 5 years and 7.75 percent interest for buy back option after 7 years. Website: www.ltinfrabond.com

Other companies and Bank such as ICICI, LIC and SBI are also likely to issue Long Term Infra Bonds soon!

Source : Helloap

New Website for Railway Online Ticket Booking

Railway Department is proposed to start a brand new website for online ticket booking. The new website is likely to be in operation with in a moth. Operational / maintenance responsibilities of the proposed new site have been taken up by SBI. Presently, IRCTC (www.irctc.co.in) is the sole seller of online tickets for Indian Railways. The proposed new portal will charge only half of the service charge that is being charged by IRCTC for online booking. Following are the service charges for the online booking through new website:

1. Reservation Charge for Sleeper Class Seat: Rs. 5
2. Reservation Charge fo AC Class Seat: Rs. 10

Railway department is also planning to integrate all its 35 websites to the new portal. IRCTC has earned Rs. 722 crores income for the last financial year. The Railway Department is gradually taking over the management of online services from IRCTC.

source :  helloap