Pointers in C

I know there are astronomical amount of articles, books, blogs have written so far on topic Pointers in C. However in this post I am trying to simplify this very topic in my own way.

To start with Pointer is a variable in C, which holds address of other variables.

image

  • Pointers can contain address of char variables
  • Pointers can contain address of int variables
  • Pointers can contain address of float variables
  • Pointers can contain NULL
  • Pointers can contain address of other pointer variables

You can create a pointer by creating a variable with the name starting with * asterisk. So a Pointer can be created as below,

image

  • First statement in above code can hold address of an integer variable.
  • Second statement in above code can hold address of a character variable
  • Third statement in above code can hold address of float variable

After declaring a pointer variable, address of simple variable can be assigned as below,

clip_image001

Different types of variable takes different space in memory. For example an integer variable will take 2 bytes, float will take 4 bytes and character variable will take 1 byte in memory. We can depict language C memory consumption by different kind of variables as below,

clip_image003

Now assume that you have created an integer pointer and assigned values to that as below,

clip_image004

There are two variables in above code snippet. Let us see how they point to different values. Assume that memory address containing value of variable var1 is 2000. You can find out from below image that var1 contains values 99 and its address in memory is 2000. Pvar1 is a pointer and it contains 2000 as value.

clip_image006

Let us go ahead and print various values.

clip_image007

One by one try to understand above lines of codes,

  1. Line 1 will print value of var1 which is 99
  2. Line 2 will print value of var1 again. Because pvar1 is pointer which contains address of var1. So to print value at that address we use * operator. Hence second line of above code will print value 99
  3. Third line will print address of var1. Address can be accessed with address operator (&)
  4. Fourth line will print address of var1. Since pvar1 is a pointer variable so printing this value will be address of the variable it contains
  5. Fifth line will print value 99. Because we are printing applying pointer variable * on address operator &.

So we can put output of above code snippets as below,

image

So I end this post with hope that you have some clarification of Pointers in C now. Code from above discussion is given below for your reference,


#include "stdafx.h"

void main()
{
int var1 =99;
int *pvar1 ;
pvar1 = &var1;

printf("%d\n",var1);
printf("%d\n",*pvar1);
printf("%u\n",&var1);
printf("%u\n",pvar1);
printf("%d\n",*(&var1));

char var2 ='a';
char *pvar2 ;
pvar2 = &var2;

printf("%c\n",var2);
printf("%c\n",*pvar2);
printf("%u\n",&var2);
printf("%u\n",pvar2);
printf("%c\n",*(&var2));

getchar();
}

Expected output of above code is as below. Have in mind that address value could be differ in your environment.

clip_image001[5]

I hope you find this post useful. Thanks for reading.

Arrays in C

Arrays are the linear data structure and stores same types of data in continuous memory location. Arrays are best used to store data in continuous memory location. However array can have definite size of data. Maximum size of an array must be set while declaring the array.

An array can be defined as below,

image

Diagrammatically myarray can be shown as below,

Note : sorry for typo error in below image, last memory location is 2064H not 2060H sorry !!!!

clip_image002

Some information about above array can be drawn as below,

  • Name of the array is myarray.
  • This is an integer array
  • Size of the array is 5
  • Base address of array is 2000H
  • One element takes 2 bytes in memory. I am assuming we are using language C, so an integer takes 2 bytes in memory.
  • 0,1,2,3,4 are index of the array
  • 99, 13,77,567,9 are elements of the array.
  • You can say 77 is array element at 2nd position of the array.
  • Notation to fetch array elements is as below,

myarray[2]=77

If you need to find memory location of 20th element in above array then you can get that using below formula,

Base Memory location + (Memory size of elements * (Element Position -1))

In above case,

  • Base memory location = 2000H
  • Size of elements = 16 Bits
  • Element position = 20

Memory location of 20th element = 2000 + (16 * (20-1)) = 2000 + 16 *19 = 2304H

On an array usually below native operations can be performed

  • Traversing an array
  • Search an element in array
  • Insert an element at given position in array
  • Delete and element from a given position in array
  • Reverse an array

Array in C

In C we can create an array as below,

clip_image002[6]

We created three different types of array. They are as below,

image

You can store values in array as below,

image

And you can print them as below,

image

Traversing an Array

Traversing an array means iterate through each element of the array once and perform some operation on each element.

image

In above pseudo code K is a variable, A is array and N is upper limit of the array. In C a function to traverse an array and print all the elements can be as below,


int  traverse(int *myarray)
{
int i;
for(i=0;i<5;i++)
{
printf("%d\n",myarray[i]);
}
return 0;
}

And in the main function array is declared as below,


#include<stdio.h>
#include<conio.h>
void main()
{
int myarray[5];
myarray[0]=9;
myarray[4]=99;
traverse(myarray);
getch();
}

You should get output as below,

image

You can see in the output that for 2nd, 3rd and 4th position elements are having some garbage value because we did not assign any values to these positions in array.

Insert in Array

You can insert an element to a given position in array following below algorithm. Let us say,

  • K: position to insert element
  • Element: element to insert
  • N : Size of the array
  • A : Name of the array

image

In C function to insert an element in given position in array can be written as below,


int insertelement(int *myarray, int position, int element)
{
int i;
for(i=4;i>=position;i--)
{
myarray[i]=myarray[i-1];

}
myarray[i]=element;
return 0;
}

And in main function you can call this function as below,


#include<stdio.h>
#include<conio.h>
#define MAX 5
void main()
{
int myarray[5];
myarray[0]=9;
myarray[4]=99;
traverse(myarray);
insertelement(myarray,2,99);
traverse(myarray);
getch();
}

When you run above program you will find 99 has been inserted at 2nd position as below,

image

You will notice all the elements after 2nd position has been shifted by one place.

Delete in Array

You can delete an element to a given position in array following below algorithm. Let us say,

  • K: position to delete element
  • N : Size of the array
  • A : Name of the array

image

In C you can write a function to delete and element from specified position as below,


int deleteelement(int *myarray,int position)
{
int i;
for(i=position; i<5; i++)
{
myarray[i-1]=myarray[i];
}
myarray[i-1]=0;
return 0;
}

Search in Array

There are two types of search you can perform on an array,

  1. Linear Search
  2. Binary Search

Binary search works on divide and conquer algorithm principal and I will take this topic separate in other blog post. Linear search can be performed as below,


int search(int *myarray, int element)
{
int i;
int flag=0;
for(i=0;i<5;i++)
{
if(myarray[i]==element)
{
flag=1;
return i+1;
break;
}
}
if(flag==0)
{
return 0;
}
}

In this way you can start working with arrays in C. in further post I will show you how to work with pointers and array together. Thanks for reading.