-1
main() {
        char names [5][20] = {
                "rmaesh",
                "ram",
                "suresh",
                "sam"
                "ramu"
        };
char *t;
        t = names[2];
        names[2] = names[3];
        names[3] = t;

        printf(" the array elemsnt are \n");
        int i = 0;
        for (i = 0; i < 5; i ++)
                printf("\n%s", names[i]);
}

i am getting below error while compiling this program

stringarrary.c: In function ‘main’:
stringarrary.c:12:11: error: incompatible types when assigning to type ‘char[20]’ from type ‘char *’
  names[2] = names[3];
           ^
stringarrary.c:13:11: error: incompatible types when assigning to type ‘char[20]’ from type    ‘char *’
  names[3] = t;
M.M
  • 138,810
  • 21
  • 208
  • 365
user1068833
  • 55
  • 1
  • 3

4 Answers4

3

It is illegal to try and assign to an array. In this case you should use the strcpy function. Note that your char *t; idea does not work either if you intend to swap the two arrays, because it only points at one of your existing strings; as soon as you write over names[2], that data is gone.

char t[20];
strcpy(t, names[2]);
strcpy(names[2], names[3]);
strcpy(names[3], t);

Also, "\n%s" should be "%s\n" - you want the newline to come after what you printed. And don't forget to #include <stdio.h> and #include <string.h>.

M.M
  • 138,810
  • 21
  • 208
  • 365
3

The error on line 13 is easiest to understand: names[3] is an array of char; you can't just assign a pointer to it. On line 12, the compiler is converting names[3] to a pointer and trying to assign it to the array names[2], which is likewise can't do.

Try copying the strings instead. In C, you can't copy arrays using the = operator; use the functions from memcpy or strcpy families.

user3553031
  • 5,990
  • 1
  • 20
  • 40
0

Try this

#include<stdio.h>
#include <string.h>
main() {
    char names [5][20] = {
        "rmaesh",
        "ram",
        "suresh",
        "sam", //<----- You are missing this (,) at the end 
        "ramu"
    };
char *t;
    strcpy( t, names[2]); // And also use this syntax
    strcpy(names[2] , names[3]);
    strcpy(names[3], t);

    printf(" the array elemsnt are \n");
    int i = 0;
    for (i = 0; i < 5; i ++)
        printf("\n%s", names[i]);
}
  • You have not allocated any memory to t. You should rather use `char t[20]` or `char *t = malloc(20)` and `free(t)` at end. Your code would cause a segmentation fault. – Mohit Jain May 04 '14 at 05:51
  • Moreover `conio.h` is not a standard file(nor is clrscr) – Mohit Jain May 04 '14 at 05:53
  • @ Mohit Jain But you can not use this `char *t = malloc(20)` syntax in `C compiler`. You could use this `char *t =(char*) malloc(20)`.:D –  May 04 '14 at 06:04
  • @Mohit Jain Moreover `getch();`. We should use these `system("cls")` and `system("pause");` thank you –  May 04 '14 at 06:11
  • You should not [typecast malloc return value to char * as suggested by you in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). Casting in the way suggested by you may leave some difficult to trace bug in your code. – Mohit Jain May 04 '14 at 06:12
0

The names array is a two-dimensional array of characters. As the other answers have pointed out, when you want to copy an array of characters, you need to use memcpy or strcpy.

The alternative solution is to make names a one-dimensional array of pointers. The resulting code would look like this.

int main( void )
{
    char *names[5] = {
        "rmaesh",
        "ram",
        "suresh",
        "sam",
        "ramu"
    };
    char *t;

    t = names[2];
    names[2] = names[3];
    names[3] = t;

    printf(" the array elemsnt are \n");
    int i = 0;
    for (i = 0; i < 5; i ++)
        printf("\n%s", names[i]);
}

The advantage to this method is that it allows you to manipulate the pointers the way you wanted to. The disadvantage is that the strings are read-only. Attempting to modify the strings will result in undefined behavior.

user3386109
  • 34,287
  • 7
  • 49
  • 68