1.
通过定义的值的时候的类型
2.
因为定义字面值指针常量没有意义,而且也无法确认该块区块无其它数据使用。
3.
值是值,值表示该地址空间类型所代表的值,需要解释为指针地址,需要特殊的&符去像变量获取地址
4.
后果为将会读取或者修改该地址的值,可能会引起整个程序的错误
5.
我感觉没啥区别
6.
有两个错误。对增值后的指针进行解引用时,数组的第一个元素并没有被清零。另外,指针在越过数组的右边界以后仍然进行解引用,还对齐进行了内容清零。
6.18编程练习:
1.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> char *find_char(char const *source, char const *chars){ int i, j; for (i = 0; *(source + i) != '\0'; i++) { for (j= 0; *(chars + j) != '\0'; j++) { if (*(source + i) == *(chars + j)) { return (char *) (source + i); } } } return NULL; } int main(void) { char str1[] = "hello"; char str2[] = "ll"; char *res; res = find_char(str1, str2); if (res) { printf("%s\n", res); } else printf("no find\n"); return EXIT_SUCCESS; }
2.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> int del_substr(char *str, char const *subchars){ if (*subchars == '\0') { return 1; } int i = 0, j = 0; for (i = 0; *(str + i) != '\0'; i++) { for (j = 0; *(subchars + j) != '\0';) { if ( *(str + i) == *(subchars + j) ) { i++; j++; } else break; if (*(subchars + j) == '\0') { goto TWO; } } } return 0; TWO: // printf("%d, %d", i, j); for ( ; *(str + i) != '\0'; i++) { str[ i -j ] = str[i]; } str[ i -j ] = '\0'; return 1; } int main(void) { char str1[] = "hello world"; char str2[] = "world"; int res; res = del_substr(str1, str2); if (res) { printf("%s\n", str1); } else printf("no find\n"); return EXIT_SUCCESS; }
3.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> void reverse_string(char *string){ char tmp; int start, end; start = 0; end = strlen(string); while (start < end) { tmp = *(string + start); *(string + start) = *(string + end - 1); *(string + end - 1) = tmp; start++; end--; } } int main(void) { char str1[] = "12"; char str2[] = "w"; reverse_string(str1); printf("%s\n", str1); return EXIT_SUCCESS; }
4.网上
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #define TRUE '1' #define FALSE '0' #define SIZE 1000 int main(void) { char num[SIZE]; int index = 0; int temp; // 赋初值 for(; index < SIZE; index++ ) { num[index] = TRUE; } // 对所有的元素进行测试 for( index = 2; index < SIZE + 2; index++ ) { if ( num[index - 2] == TRUE ) { for(temp = index+1; temp <= SIZE + 2; temp++ ) { if( num[temp -2] == TRUE && temp % index == 0 ) { num[temp - 2] = FALSE; } } } } // 输出 int number = 0; for( index = 0; index < SIZE; index++) { if( num[index] == TRUE ) { if( number % 5 == 0) printf( "\n" ); printf("%d\t", index + 2 ); number++; } } printf("\n"); return EXIT_SUCCESS; }
5.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #define SIZE 1000 #define MAX_VALUE (SIZE * CHAR_BIT) /* 对字符数组的位操作 */ unsigned bit_offset( unsigned bit_number ) { return bit_number % CHAR_BIT; } unsigned character_offset ( unsigned bit_number ) { return bit_number / CHAR_BIT; } // 测试指定位为1还是为0 int test_bit( char bit_array[], unsigned int bit_number ) { return ( bit_array[ character_offset( bit_number ) ] & 1 << bit_offset( bit_number )) != 0; } //清零 void clear_bit( char bit_array[], unsigned int bit_number ) { bit_array[ character_offset( bit_number )] &= ~(1 << bit_offset( bit_number )); } int main(void) { // 定义SIZE数量字节的数组 char sieve[ SIZE ]; // 初始化所有的位为1 for( int i = 0 ; i < SIZE ; i++) sieve[i] = 0xff; // 对所有的位进行判断 起始位从数值2开始 for( int number = 2; number < MAX_VALUE + 2; number++ ) { // 测试指定位是否为质数 if (test_bit(sieve, number-2)) { for (int j = number + 1; j < MAX_VALUE + 2; j++) { if (test_bit(sieve, j-2) && j % number == 0) { clear_bit(sieve, j-2); } } } } for(int i = 0; i < MAX_VALUE; i++ ) { if( test_bit( sieve, i )) printf("%d\t", i + 2); } putchar('\n'); return EXIT_SUCCESS; }
7993
6.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #define TRUE '1' #define FALSE '0' #define SIZE 1000000 int main(void) { char num[SIZE]; int index = 0; int temp; // 赋初值 for(; index < SIZE; index++ ) { num[index] = TRUE; } // 对所有的元素进行测试 for( index = 2; index < SIZE + 2; index++ ) { if ( num[index - 2] == TRUE ) { for(temp = index+1; temp <= SIZE + 2; temp++ ) { if( num[temp -2] == TRUE && temp % index == 0 ) { num[temp - 2] = FALSE; } } } } // 输出 int number = 0; for( index = 0; index < SIZE; index++) { if( num[index] == TRUE ) { number++; } if ((index+2) % 1000 == 0) { printf("index <= %d\t, number=%d\n", index+2, number); number = 0; } } printf("\n"); return EXIT_SUCCESS; }
我的电脑跑了3分钟出来结果,感觉是越来越少的状态