随笔分类 - c/c++
摘要:c语言中字符串的复制。 1、自定义函数 #include <stdio.h> char *str_copy(char *d, char *s) { char *t = d; while(*d++ = *s++) ; return t; } int main(void) { char str[128]
阅读全文
摘要:1、 #include <stdio.h> char *str_chr(char *x, int key) { while(*x) { if(*x == key) { char *t = x; return t; } x++; } return NULL; } int main(void) { ch
阅读全文
摘要:1、 #include <stdio.h> int str_c(const char *x, int key) { int j = 0; while(*x) { if(*x == key) j++; x++; } return j; } int main(void) { char str[128];
阅读全文
摘要:1、 #include <stdio.h> void put_string(const char *s) // 传入的实参为指针,在数组中,指向首个字符的指针的行为和数组本身一样。 { printf("%s\n", s); // 显示数组本身。 } int main(void) { char str
阅读全文
摘要:1、利用数组。 #include <stdio.h> int len(char x[]) { int len = 0; while(x[len]) len++; return len; } int main(void) { char str[128]; printf("str = "); scanf
阅读全文
摘要:1、 #include <stdio.h> char* copy_str(char *d, const char *s) { char *t = d; while(*d++ = *s++) ; return t; } int main(void) { char str[128] = "ABCD";
阅读全文
摘要:c语言中字符串的复制。 1、 #include <stdio.h> char* str_copy(char *d, const char *s) //函数的返回值为指向char型的指针型, 形参为两个指向char型的指针。 { char *t = d; // 指针t等于指针d,指针d为指向字符串第一
阅读全文
摘要:c语言中利用数组、指针实现字符串、字符串数组。 1、字符串 #include <stdio.h> int main(void) { char x[] = "abcd"; //数组实现字符串 ,x一般解释为指向数组第一个元素的指针 char *y = "xyz"; //指针实现字符串 ,y为指向一个字
阅读全文
摘要:c语言中显示字符串数组的长度(数组实现的字符串数组和指针实现的字符串数组) 1、 #include <stdio.h> int main(void) { char x[][128] = {"aaa","bb","cccccc","d","eee"}; char *y[] = {"11111","22
阅读全文
摘要:c语言中判断字符串的长度,利用数组和利用指针。 1、利用数组。 #include <stdio.h> int len(const char x[]) { int len = 0; while(x[len]) len++; return len; } int main(void) { char str
阅读全文
摘要:1、 #include <stdio.h> int main(void) { int i; char a[][5] = {"LISP","C","Ada"}; char *p[] = {"PAUL","X","MAC"}; printf("sizeof(a) = %u\n", sizeof(a));
阅读全文
摘要:1、 #include <stdio.h> int main(void) { int i; char a[][5] = {"LISP","C","Ada"}; char *p[] = {"PAUL","X","MAC"}; int j = 0; while(1) { int k = 0; while
阅读全文
摘要:1、 #include <stdio.h> int main(void) { char *p = "123"; printf("p = %s\n", p); p = "456"; printf("p = %s\n", p); return 0; } ↓ #include <stdio.h> int
阅读全文
摘要:用数组实现的字符串和用指针实现的字符串。 1、 #include <stdio.h> int main(void) { char str[] = "abc"; //数组实现的字符串,str为数组名,为指向数组起始元素的指针,相当于“a”的指针。 char *ptr = "123"; //指针实现的字
阅读全文
摘要:1、 #include <stdio.h> void ary_set(int x[], int n, int val) { int i; for(i = 0; i < n; i++) { x[i] = val; } } int main(void) { int i; int a[5] = {1, 2
阅读全文
摘要:1、 #include <stdio.h> void assign1(int x[], int n)//函数间数组的传递是以数组第一个元素的指针的形式传递的,因此形参变为指向数组第一个元素的指针,形参和数组本身的行为一样。 { int i; for(i = 0; i < n; i++) { x[i]
阅读全文
摘要:1、函数间数组的传递是以指向第一个元素的指针的形式进行的,应为数组名会被解释为指向数组第一个元素的指针,形参部分的指针变量被赋值为数组的第一个元素 的指针时,指针变量指向数组的一个元素,因此指针变量的行为和传入的数组一样。 #include <stdio.h> void assign(int x[]
阅读全文
摘要:对于数组a[n], 当p为数组的起始元素的指针时, 即 p = a = &a[0]时, p + i = a[i], &p[i] = &a[i] , a[i] = p[i], *(a + i) = *(p + i), 也就是说当p为数组的起始 元素的指针时,p的行为和数组本身是一样的。(原因: p为数
阅读全文
摘要:当指针p声明为数组名a时, 则有 p + i = &a[i], 也就是说p + i是首个元素之后的第i个元素的指针。 1、 #include <stdio.h> int main(void) { int i; int a[5]; int *p = a; for(i = 0; i < 5; i++)
阅读全文
摘要:1、三个值升序 #include <stdio.h> void swap(int *x, int *y) { int tmp = *x; *x = *y; *y = tmp; } void sort(int *i, int *j, int *k) { if(*i > *j) swap(i, j);
阅读全文

浙公网安备 33010602011771号