Array and Pointers Arithmetic in C Programming – Examples

Array and Pointers Arithmetic in C Programming – Examples

Pointers

Pointer variable holds the address of another variable of same data type, means the address of integer type variable can be placed in integer type pointer variable. It contains memory addresses as their values. accessing a value through a pointer is called indirection. pointer variable must be declared before it can be used.

 

Download Code :  Basic-pointer1.c (38 downloads)

In example above, x is integer type variable holds values 10. p is integer type pointer variable.

& is address of operator which return the address of variable.

p=&x; it means store the address of variable x in pointer variable p.

*  is indirection operator which return the value stored at memory address held by pointer variable.

*p return the value stored at variable x.

p=&x means assign the address of x variable to pointer variable p. i.e pointer p points to x location. Now we can access or update the value of x using pointer p;

OUTPUT
output-simple-pointer
output-simple-pointer

Pointer Arithmetic – Array and Pointer Variable:

Basically array variable is constant pointer; i.e the value of pointer variable can not be updated. but this Array variable value which is the address of first element of array can be assigned to other pointer variable.

Download Code Pointer-Arithmetic-pointer2.c (30 downloads)

In example above, x is array of 5 integer variables which initialized to 100, 200, 300, 400, 500 respectively. p is integer type pointer variable.

p=x; this statement assigns the value of x which is address of first element, assigned to pointer variable p. this pointer variable can be incremented or decremented.

p=p+2; When an integer is added to or subtracted from a pointer, the new pointer value of p is changed by the integer times the number of bytes in the data variable. initially p points to x[0] location , after p = p + 2 , new p starts pointing to x[3].

OUTPUT
output-array-pointer
output-array-pointer

Char Array and Pointer Variable -String length and String Lower case – Example:

Download Code: string-length-and-lower-case-using-pointer.c (48 downloads)

In example above, p is a character pointer points to character array. Here using indirection operator we access the character stored at first position in array and check for null character, if not null character then increment counter and modify its ascii code by adding 32 value. so ascii code of upper case letter get converted to ascii code of lower case letter. p++ , Increments pointer p by 1, so its pointing to next character in array repeat steps from null character check till null character.

OUTPUT
output-stringlower-using-pointer
output-string-lower-using-pointer

Char Array and Pointer Variable – String Copy Example

Download code:  string-copy-using-pointer.c (38 downloads)

In example above, src (as source) and dst (as destination) are char array of size 50. src array holds string ABCDE. To copy these contents from src array to dst array using pointer, here two char pointer variables are declared as p and q which assigned to src and dst array. till null character copy character from pointer p to pointer q using indirection operator. increment both pointer for next position. finally add null character explicitly in dst array. 

 

OUTPUT
output-string-copy-using-pointer
output-string-copy-using-pointer