13 July 2015

APPLY THESE 10 SECRET TECHNIQUES TO IMPROVE POINTER IN C PROGRAMING

Pointer in c in detailed
Hi friends, today I am going to write about pointers for C programming language.Pointers are most confusing but most important quality of C and C++ language.it gives the strength to C language to communicate and access hardware resources. In the advanced level of C, programming pointers play very important role.Along with these, it is always an important topic for completion technical exam.So iI stop pointing advantage of it and explain each and every point related to pointer or Pointer in depth.


Advantage of pointer

1) It makes you able to access any memory location in the computer's computer.
2) We can return multiple values from function using pointers.

Usage of pointer

There are many usages of pointers in C language.

1) Arrays, Functions, and Structures

Pointers in c language are widely used in arrays, functions and structures. It reduces the code and improves the performance.

You can learn basic C programming language from this post 5 best website to learn C Programming

1)What is pointer:

Pointer are variable like other in C programming but in different it stores address of variable.
It is called pointer because it points to a particular location in memory by sorting the address of that location.
Like all other variables it also has a name, has to be declared and occupies some spaces in memory.

2)The general syntax of declaration of Pointer in C:

Data-type * pname;
Here pname is the name of pointer variable which should be a valid C identifier.
The Astrix * preceding this name inform the compiler that the variable is declared as a pointer.

int * iptr;

iptr is a pointer that should point to a variable of type int.You can make space between Astrix and variable name or not provide space between data type and Astrix ex. int* iptr.I have checked this in Turbo C.So if you get other result, comment below.
Since pointers has a variable it will occupy some space in memory.
All pointers contain address only, generally two byte are used to store an address.Since all address will unsigned therefore it is use %U in printf() function as data format.

3)Assigning address to pointer variable:

take an example
int * iptr,age=30;
Float * ftpr,sal=1500.00;
iptr=&age;
fptr=&sal;
When we declare a pointer variable iptr,i.e it may be pointing anywhere in the memory.so we should always assign an address before using it in the program.
Pointer concept in c brief
Note: you can't Assign a value at declaration time of a Pointer.You can see this in next Post Video

4)How we can extract value from pointer:

printf("%d",*iptr); if base data type of pointer is integer,If float then use %f,If character then %c etc.

5)Pointer Arithmetic:

The only valid operation that can perform are as
1.Addition of an integer to pointer and increment operation
2.Subtraction of an integer from pointer and decrement operation
3.Subtraction of a pointer from another pointer of same type

All arithmetic is performed relative to the size of  the base type of pointer.

Ex- If we have an integer pointer pi which contain address 1000 Then on incrementing, we get 1002 instead of 1001. Because a base type of pointer is int and it takes a space of 2 byte(In Turbo C, float 4 byte, char 1 byte and so on.You can know more about C Data Type.
Note: The precedence level of  * operator and incremental/decremental operation is same and their associativity is from right to left.
Ex X=*ptr++ is equivalent to *(ptr++).increment is applied to ptr not to * ptr.

6)Assigning pointer to other pointers:

It is possible to assign the value of one pointer variable to another,but provides their base type is same.
Ex int *pi, *iptr,a=3;
iptr=&a;
pi=iptr;
pi=iptr //by doing this both pointer indicates same variable a and we print their value by *pi then we                //get 3
Note:In pointer, we access variables by using & i.e, stored the address of variable in pointer, operator and extract value by *(value at)operator.but when we want to access pointer by pointer,then we declare as above.In this the value of iptr pointer store in the pi pointer nor address of iptr pointer.If you want to access iptr pointer by &(address of) operator then you have to declare pi pointer as int **pi.It is read as Pointer of pointer pi.
pointer in depth




7)NULL Pointer:

 A Pointer that assigned NULL is called a null pointer. It  is always a good practice to assign a NULL value to a pointer variable in case you don't have exact address to be assign.
EX. int *ptr=NULL; The value of this pointer is zero.You can also declare as (void*)0;

8)Typecasting of pointer:

This operation requires when we want to tell the compiler that return pointer is having which data type.When we do dynamic array Creation or programming then we require it.We will provide information in upcoming post.
EX-
Q. In a C program with the function (int *) malloc (sizeof(int)), what does (int *) mean?
A. If you write it properly. it would be this--> (int *) malloc(sizeof(int)) . Here (int *) is used to typecast the pointer returned by malloc(). malloc() function returns null pointer. Type casting means to convert a variable from one data type to another data type.
Note: We can assign a pointer which has return data type into Pointer which has void return data type.
Ex: void *voidptr;
int *ptr;
voidptr=ptr;


9)An unassigned pointer returned garbage address. So alway try to declare and initialize the pointer.

10)You can test your pointer concept here.

Example of a simple program:

pointer in c programming language in detailed

Update: For better conception, there is a video for you. Pointers in C:basic and how to use Pointers

.

11.) We can update a variable with help of pointer.Look this code
   int *a,b;
     a=&b;
    *a=10;
   printf("%d %d",*a,b);
Output 10 10
Reason: Since *a=10, Then *(&b)=10
and one thing noted that we can also get value from non-pointer variable as *(&variable).
So *(&b)=b=10.
   



No comments:

Post a Comment

Ads Inside Post