字符指针的操作
#include <stdio.h>
int main() {
char str[] = "hello world";
printf("str = %s\n", str);
printf("str = %p\n", str);
char *p = str;
*p = 'm';
p++;
*p = 'i';
printf("str = %s\n", str);
printf("p = %s\n", p);
p = "faye";
printf("str = %s\n", str);
printf("p = %s\n", p);
char *q = "haha";
q++;
printf("q = %s\n", q);
int *arr = {111, 222, 33};
printf("arr = %d\n", arr);
return 0;
}
字符串拼接案例
#include <stdio.h>
void mystrcat(char *dest, const char *src){
int len1 = 0;
int len2 = 0;
while (dest[len1]){
len1++;
}
printf("len1 = %d\n", len1);
while (src[len2]){
len2++;
}
printf("len2= %d\n", len2);
dest[len1] = src[0];
dest[len1+1] = src[1];
dest[len1+2] = src[2];
for (int i = 0; i < len2; i++) {
dest[len1+i] = src[i];
}
}
int main() {
char dst[100] = "hello faye";
char src[] = "123456666";
mystrcat(dst, src);
printf("%s\n",dst);
return 0;
}
const 修饰指针变量
#include <stdio.h>
int main() {
const int a = 10;
char buf[] = "asdfghjkl";
const char *p = buf;
p = "ddddddddd";
printf("p = %s\n",p);
printf("buf = %s\n",buf);
char * const p2 = buf;
p2[1] = 'x';
printf("p2 = %s\n",p2);
printf("buf = %s\n",buf);
const char * const p3 = buf;
return 0;
}
- 有个数组,找出第二大的数,并且打印出来(使用*操作数组元素个数,不要使用[])
int a[] = {5,100,32,45,21,67,32,68,41,99,13,71};
- 不使用库函数,实现字符串拷贝功能
char src[100] = “hello”; char dst[100]; //把src的内容拷贝给dst
#include <stdio.h>
int main() {
char src[100] = "hello";
char dst[100];
char *p1 =src;
char *p2 =dst;
p2 = p1;
printf("%s \n", p2);
printf("%s \n", dst);
return 0;
}
|