Introduction
Pointers are a part of the C language and make the C language more powerful. The following arre some features ot them:
- A pointer mainly represents a data structure.
- It changes values of an actual argument passed to the function (“call by reference”).
- It is also for working with memory allocation dynamically.
- It is efficiently used with integer arrays and character arrays.
- A pointer always stores a memory address, not data values.
- A pointer holds the value aa an address.
Declaration
A pointer variable must be declared before use. The syntax for a pointer declaration is as follows.
Int *a;
Double *b;
Now a is a “pointer to an integer”and b is a “pointer to a double”.
The prefix * defines the variable as a pointer.
Example
- Int *a;
- Int b;
- a=&b;
Arithmetic operation are limited to the following for pointers:
- Integers and pointers can be added and subtracted.
- Incremented and decremented
- In addition, pointers can be assigned to each other
Example
- Int *a,*b;
- a=a+5;
- b=a;
Using call by reference means that a parameter is sent to the function in the form of an address location.
- #include<stdio.h>
- Void swap(int *p , int *q);
- main()
- {
- Int i=3,j=5;
- swap( &I , &j ); //sentind address of I and j variables
- printf("After swap, i=%d j=%d\n" ,I ,j );
- getch();
- return 0;
- }
- Void swap(int *p, int *q)
- {
- Int temp;
- temp=*p;
- *p=*q;
- *q=temp;
- }
After swap, I=5 j=3
Pointers and arrays
A pointer always contains the base address of the memory location that makes up an entire array.
Declaration
Scanf(“%s” ,name);
Example
- Int a[5];
- a[5]=10;
- *(a+5)=56;
All of those are the same thing.
Examples
-
- Int a[100] , I , *p , sum=0;
- for (i=0 ; i<100 ;++I )
- Sum += a[i];
-
- int a[100] , I , *p , sum=0;
- for (i=0 ; i<100 ; ++I )
- Sum += *( a + I );
-
The following shows an array as function arguments:
- int a[100] , I , *p , sum=0;
- for ( p=a ; p<&a[100] ; ++p )
- sum += *p;
- double sum(double *dp , int n )
- {
- Int I ;
- Double res=0.0 ;
- for ( i=0 ; i<n ; ++I )
- res += *( dp + I ) ;
- return res;
- }
That function needs a starting address in the array and the number of elements to be summed together.
Pointers and Character Strings
- #include<stdio.h>
- main()
- {
- Char *cp ;
- cp = "Neeraj Kumar";
- printf("%c\n" , *cp );
- printf("%c\n" , *( cp + 7 ));
- return 0;
- }
N
K
We can use pointers to work with character strings. The value of the string constant address is the base address of the character array.
Thank you.

Chiheb ChebbiPosted Jun 9, 2015, 8:52 AM
Thank you
Neeraj KumarPosted Jun 6, 2015, 2:50 AM
Thank you Santhakumar Munuswamy
Santhakumar MunuswamyPosted Jun 6, 2015, 2:47 AM
Thanks for nice article
Neeraj KumarPosted Jun 5, 2015, 12:46 PM
Hi Danny Schneider, i know some error in the my article of naming problem and missing of * symbol in arithmetic Pointer but that problem i'll not repeat again.Thank you for your advice
Danny SchneiderPosted Jun 5, 2015, 9:26 AM
Hi Neeraj Kumar, I think the article is a bit confusing because the in my opinion it is hard to read. The variable namings are not so good, because inside the text the meaning of 'a' or 'b' get lost. I'm a bit familiar to C/C++ and I think the description of the pointer arithmethic is too lazy. A beginner will not undersrand what he will do if he adds 5 to a pointer variable.
NitinPosted Jun 5, 2015, 4:32 AM
nice
Sibeesh VenuPosted Jun 5, 2015, 1:43 AM
Good one.
Gopi ChandPosted Jun 4, 2015, 11:56 PM
Good one