This is my code in which I have created an array of pointers. The array of pointers holds the base address of the string.I have created add function by which I am adding the strings to the array of pointer.My motto is to swap the first character of the two strings "akshay" and "raman".For example ,"akshay" after swapping should become "rkshay" and "raman" after swapping should become "aaman" i.e. a of akshay should be replaced by r of raman and vice-versa. But,when I am executing it shows error like "A problem caused the program to stop working correctly.Windows will close the program and notify if a solution is available." Please provide a solution.
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define MAX 6
char *names[MAX];
int count;
int add(char *);
void swap(int,int);
void show();
int main()
{
int flag;
flag=add("akshay");
if(flag==0)
printf("unable to add string\n");
flag=add("parag");
if(flag==0)
printf("unable to add string\n");
flag=add("raman");
if(flag==0)
printf("unable to add string\n");
printf("names before swapping \n");
show();
swap(0,2);
printf("names after swapping \n");
show();
return 0;
}
/*adds given string */
int add(char *s)
{
if(count<MAX)
{
names[count]=s;
count++;
return 1;
}
else return 0;
}
/*swaps the first characters of the two strings */
void swap(int i,int j)
{
char temp;
temp=(*names[i]);
*names[i]=(*names[j]);
*names[j]=temp;
}
/* displays the elements */
void show()
{
int i;
for(i=0;i<count;i++)
{
puts(names[i]);
printf("\n");
}
}