0
int main(){

char* ants[5]={"red ant" ,"blue ant" ,"green ant" ,"yellow ant" ,"white ant"};

.... }

how can I swap two strings if I want to swap "red ant" and "white ant" ? can someone help me write the code (without using another array at all)???

Barmar
  • 741,623
  • 53
  • 500
  • 612
nisdsccd
  • 17
  • 7

2 Answers2

4

The strings used for the types of ants are String-Literals and on all but non-conforming compilers are read-only (non-mutable). A string literal has the type array of characters with the length set to contain the characters and the '\0' (nul-terminating characer). The standard section for string literals is C11 Standard - 6.4.5 String literals

You cannot change the contents of the string literals, but you can change which pointer points to which string-literal. In your case you have:

    char *ants[5]={"red ant" ,"blue ant" ,"green ant" ,"yellow ant" ,"white ant"};

Which has the type Array of Pointers to char. So you have an array of character pointers. Each of the pointers in the array holds the address of one of the strings in ants. To change the order of the strings in ants you have to swap the addresses held by the pointers in your array.

A trivial example to swap the red and blue ants could be:

#include <stdio.h>

int main(){

    char *ants[5]={"red ant" ,"blue ant" ,"green ant" ,"yellow ant" ,"white ant"};
    int n = sizeof ants / sizeof *ants;         /* number of ants */
    
    char *t = ants[0];                          /* temp pointer holding 1st address */
    ants[0] = ants[1];                          /* assign 2nd address to 1st pointer */
    ants[1] = t;                                /* assign temp pointer to 2nd */
    
    for (int i = 0; i < n; i++)                 /* output swapped ants */
        puts (ants[i]);
}

Example Use/Output

$ ./bin/swap_ants
blue ant
red ant
green ant
yellow ant
white ant

If you wanted to write a swap() function to swap the pointers, you cannot just pass the pointers as arguments, e.g., you could NOT write:

swap (char *a, char *b)

and then swap a and b in the function. Why? The pointers a and b are copies of the actual pointers. They are local variables for the swap() function. Therefore, nothing you do to them in the function would be seen back in main().

You CAN provide the address of the pointers as the arguments and then swap the pointers at those two addresses and the changes WILL be seen back in main(). By passing the actual address of the original pointer, any changes made are made to the values at the original address and are available back in main(). So a swap() function for two pointers could be written as:

/** swap pointers, changing pointer at address */
void swapptr (char **a, char **b)
{
    char *t = *a;           /* pointer at a's address saved in t */
         *a = *b;           /* assign pointer from b to a */
         *b = t;            /* assign temp pointer t to b */
}

Now if you wanted to swap the red and blue ants, you could simply call the swapptr() function passing the address of each of the pointers, e.g.

#include <stdio.h>

/** swap pointers, changing pointer at address */
void swapptr (char **a, char **b)
{
    char *t = *a;           /* pointer at a's address saved in t */
         *a = *b;           /* assign pointer from b to a */
         *b = t;            /* assign temp pointer t to b */
}

int main(){

    char *ants[5]={"red ant" ,"blue ant" ,"green ant" ,"yellow ant" ,"white ant"};
    int n = sizeof ants / sizeof *ants;         /* number of ants */
    
    swapptr (ants, ants + 1);
    
    for (int i = 0; i < n; i++)                 /* output swapped ants */
        puts (ants[i]);
}

(same output)

Note: you have to pay attention to the types. ants (which is ants+0), and ants + 1 are pointers to your string literals, they are pointers to an array of chars. But on access, an array of Type in C is converted to a pointer to the first element. C11 Standard - 6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3) So each and is a pointer to a pointer to the first character in each. So each element has the type char** to begin with. So that's why you can pass ants + 0, ants + 1 to swap the first and second (red and blue) ants.

Look things over and let me know if you have further questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
2

As far as I know a char* is a POINTER to an adress record. So if you define char *temp; and use this variable to store the pointer to ants[0] you technically didn't create a different array to create the new order.

char *temp;

temp=ants[0];
ants[0]=ants[4];
ants[4]=temp;

And the places are exchanged...