Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

how can I swap two strings in a string-array of pointers in C without using other arrays?

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)???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 '' (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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...