如何在不使用其他数组的情况下在C的指针的字符串数组中交换两个字符串? [英] 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)???
推荐答案
用于蚂蚁类型的字符串是 String-Literals ,除不符合标准的编译器外,其他所有编译器都是只读的(非-可变).字符串文字的类型为字符数组,其长度设置为包含字符和'\ 0'
(以零结尾的字符).字符串文字的标准部分为 C11标准-6.4.5字符串文字
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"};
类型为指向的指针数组 char
.因此,您有一个字符指针数组.数组中的每个指针都保存 ants
中字符串之一的地址.要更改 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]);
}
使用/输出示例
$ ./bin/swap_ants
blue ant
red ant
green ant
yellow ant
white ant
如果您想编写 swap()
函数以交换指针,则不能仅将指针作为参数传递,例如,您不能编写:
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)
,然后在函数中交换 a
和 b
.为什么?指针 a
和 b
是实际指针的副本.它们是 swap()
函数的局部变量.因此,在 main()
中将看不到您在函数中对其执行的任何操作.
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()
.
您可以提供指针的地址作为参数,然后在这两个地址处交换指针,并且更改将在 main()
中被看到.通过传递原始指针的实际地址,可以对原始地址处的值进行任何更改,并且可以在 main()
中使用.因此,用于两个指针的 swap()
函数可以写为:
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 */
}
现在,如果您想交换红色和蓝色的蚂蚁,您可以简单地调用 swapptr()
函数,传递每个指针的地址,例如
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]);
}
(相同的输出)
注意:您必须注意类型. ants
(即 ants + 0
)和 ants + 1
是指向字符串文字的指针,它们是指向chars数组的指针.但是在访问时,C语言中的Type数组将转换为指向第一个元素的指针. C11标准-6.3.2.1其他操作数-左值,数组和函数标识符(p3)因此,每个都是指向每个字符中第一个字符的指针.因此,每个元素的开头都是 char **
类型.所以这就是为什么您可以传递 ants + 0,ants + 1
来交换第一和第二(红色和蓝色)蚂蚁的原因.
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.
这篇关于如何在不使用其他数组的情况下在C的指针的字符串数组中交换两个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!