[C][代码实例]交换指向常量的二级指针的位置
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #define ARR_LEN 3 #define STR_LEN 3 void swap(const char**, const char**); int main( void ) { char str_1[ARR_LEN][STR_LEN] = { "ap\0", "br\0", "dr\0" }; const char **Pstr_1 = malloc( sizeof( char* ) * 3 ); int i = 0; for (; i < ARR_LEN; ++i) { //*( Pstr_1 + i ) = malloc( sizeof( char ) * 3 );//你可以给指针分配自己的空间 *( Pstr_1 + i ) = *( str_1 + i );//但是现在我们先复制str_1的指针给它们 } i = 0; for (; i < ARR_LEN; ++i) { printf( "%s\n", *( Pstr_1 + i ) ); } swap((Pstr_1 + 0), (Pstr_1 + 1)); i = 0; for (; i < ARR_LEN; ++i) { printf( "%s\n", *( Pstr_1 + i ) ); } return 0; } void swap(const char **p1, const char **p2) { const char *pTemp = *p1; *p1 = *p2; *p2 = pTemp; }