您的位置:首页 > 其它

关于字符串类型与字符数组(指针)

2018-04-01 22:07 423 查看
又在字符串上栽了个跟头,上次也在链表指针上弄了个晕头转向:
 void swap(char*a, char*b) {
char temp[10];
//strcpy只会赋值有存储空间的字符串
strcpy(temp, a);
strcpy(a,b);
strcpy(b, temp);
}
void PrintStr(char str[][10],int n) {
for (int i = 1; i <= n; i++) {
if (i == n)	printf("%s\n", str[i]);
else printf("%s ", str[i]);
}
}
void swap2(char**a, char**b) {
char*temp;
temp = *a;
*a = *b;
*b = temp;
}
//此时字符串可以完全当做一个变量来使用
int main() {
char str[100][10] = { "dsfsd","come","sdfg" };
swap(str[0], str[1]);
PrintStr(str, 3);
char*a = "dfdf";
char*b = "aaad";
swap2(&a, &b);//此交换只是复制的另外一份
printf("%s  %s", a, b);
return 0;
}
总而言之,就是指针变量相当于一个新的变量,在函数中必须建一个指针变量指向这个指针变量才能改变这个变量的值
忘记的话尝试就尝试一下上面的代码#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void swap2(char**a, char**b) {
char*temp;
temp = *a;
*a = *b;
*b = temp;
}
void permut(char**str, int start) {
if (start == 4) {
for (int i = 0; i < 4; i++) printf("%s\t", str[i]);
printf("\n");
}
for (int j = start; j < 4; j++) {
swap2(str+start, str+j);
permut(str, start + 1);
swap2(str + start, str + j);
}
}
int main() {
char*str[] = { "I'm","a","big","god"};
permut(str, 0);
return 0;
}以上是测试的字符串数组的全排列。
字符串(也泛指其它指针变量)可以用malloc和new来给一个赋值空间,但其还是按照一个指针变量来处理
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐