0

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");
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    You haven't allocated memory for the strings... – xrisk Sep 04 '17 at 18:38
  • 6
    You are storing pointers to [string literals](https://stackoverflow.com/questions/2245664/what-is-the-type-of-string-literals-in-c-and-c), as such you cannot modify the strings themselves. – Weather Vane Sep 04 '17 at 18:38
  • 1
    ..and if you were not adding string literals, storing just the string addresses in a pointer array, (as distinct from allocating space and copying the strings, eg. with strdup), is dubious at best, since the valid lifetime of the pointer targets is dependent on the caller. – Martin James Sep 04 '17 at 18:56
  • Sidenote: an "array of pointers" cannot "store strings" for the obvious reason it stores pointers and a pointer is not an array which C strings are actually. (and get a better C book; your use of parentheses complicates reading your code). – too honest for this site Sep 04 '17 at 19:30
  • fix like [this](https://ideone.com/qrwqik) – BLUEPIXY Sep 04 '17 at 20:20

1 Answers1

0

How can I swap the first character of two strings with each other ?

The function can look the following way

void swap( char *s1, char *s2 )
{
    if ( *s1 && *s2 )
    {
        char c = *s1;
        *s1 = *s2;
        *s2 = c;
    }
}

As for your program then you are trying to modify string literals that results in undefined behavior.

From the C Standard (6.4.5 String literals)

7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

You should dynamically allocate memory for each added string to your array of pointers.

Also it is a bad idea when functions rely on global variables.

Take into account that according to the C Standard the function main without parameters shall be declared like

int main( void )
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335