# C -Pointers
## What is pointers
A pointer is a variable which store address of other variable of same data type.
**How to declare a pointer**
>data-type *variable_name;
>eg.
>int *ptr;
>
**How to initialize a pointer**
>int x = 20 ; //declaration and initialization of a variable of int type
>int *ptr = &x; //initialize a int type pointer with an address of x
>
>Note: (&) act as 'address of'

## Small programme to illustrate use of pointers and accessing address of a variable
>#include<stdio.h>
void main()
{
int x=20;
int *ptr=&x;
printf("x is stored at %u\n",&x); // '&x' gives address of variable x
printf("x is stored at %u\n",ptr); //ptr contains address of x
printf("value of x is %d\n",x);
printf("value of x is %d\n",\*(&x)); // '\*' is used to access value prersent at given address which in this case is '&x'
printf("value of x is %d\n",\*(ptr)); //as ptr contains address of x so'\*(ptr)' will give value of the address stored in ptr
}
**output**

## Pointer expression
y= *p1 * *p2; //same as (*p1) * (*p2)
y= *p1 + *p2;
y= y* - *p2/ *p1; //same as (y * (-(*p2)))/(*p1), space between '/' and '\*' is important
## Pointer incriment and scale factor
suppose address stored in pointer p of int type is 2500.
p++ will cause the pointer value to be incrimented by the length of the data type it points to (i.e. new address p points to is '2504' not '2501') and this length is called scale factor.
So you can say p = p + 2 is equivalent to
p = p + (2 * (scale factor of int))
p = 2500 + (2 * 4)
p = 2508
**Scale factor of different data-type for 64-bit machines**
char 1 byte
int 4 bytes
float 4 bytes
long int 8 bytes
double 8 bytes
## Arrays and Pointers
An array name itself is a constant pointer pointing to the first element.
>int x[5] = {100,200,300,400,500};

Here name x is a constant pointer pointing to the the first element x[0], so value of x is 2500.
x = &x[0]
x = 2500
You can assign a pointer to first element of x[0] as
> int *ptr = x;
> or
> int *ptr = &x[0];
by incrimenting ptr we can access every index of array x.
ptr = &x[0] = 2500
ptr + 1 = &x[1] = 2504
ptr + 2 = &x[2] = 2508
ptr + 3 = &x[3] = 2512
ptr + 4 = &x[4] = 2516
to access the value you have to use '\*'
\*(ptr) = x[0] = 100
\*(ptr + 1) = x[1] = 200
\*(ptr + 2) = x[2] = 300
\*(ptr + 3) = x[3] = 400
\*(ptr + 4) = x[4] = 500
Let's see a programme to traverse an array using pointer.
>#include<stdio.h>
void main()
{
int x[5] = {100,200,300,400,500};
int \*ptr = x; //assigning base address of an array x to ptr
int i;
for(i=0; i<5; i++)
{
printf("%d ",\*(ptr+i));
}
}
**Output**

we can also manipulate a 2-D array using pointers

where, **\*(\*(ptr + i) + j) = x[i][j]**
There is also a simple way to handling a pointer to 2-D array, i.e. **ptr[i][j] = \*(\*(ptr + i) + j) = x[i][j]**
## Strings and Pointers
Just like array we can manipulate a string using pointers. All the basics remain same as array.
Pointers to an character array declared and initialized as
>char str[5] = "abcd"; // last cell is reserved for '\0' character
>char *s = str; // assigning base address of str to pointer s

We can also declare a string using pointer variable of type char, e.g.
>char *s = "abcd";
This will create an string and store its base address to the pointer s.

Small programme to illustrate use of pointers on character array
>#include<stdio.h>
#include<string.h>
void main()
{
char str[7]="string";
char \*s = str;
puts(s);
char \*s1 = "string2";
puts(s1);
}
