这是个人对该在Linux平台上的class="tags" href="/C.html" title=C>C语言学习网站笨办法学class="tags" href="/C.html" title=C>C上的每一个练习章节附加题的解析和回答
<code class="language-cs">if(isalpha(ch) || isblank(ch)) {printf("'%c' == %d ", ch, ch); }code>
<code class="language-cs">#include <stdio.h> #include <ctype.h> #include <string.h>// forward declarations int can_print_it(char ch); void print_letters(char arg[],char str_len);void print_arguments(int argc, char *argv[]) {int i = 0;for(i = 0; i < argc; i++) {print_letters(argv[i],strlen(argv[i]));} }void print_letters(char arg[],char str_len) {int i = 0;for(i = 0; i < str_len; i++) {char ch = arg[i];if(isalpha(ch) || isblank(ch)) {printf("'%c' == %d ", ch, ch);}}printf("\n"); }int main(int argc, char *argv[]) {print_arguments(argc, argv);return 0; } code>
<code class="language-cs">#include <stdio.h> #include <ctype.h> #include <string.h>// Function prototypes void print_arguments(int argc, char *argv[]); void print_letters(const char *arg); void print_digits(const char *arg); void print_punctuation(const char *arg);void print_arguments(int argc, char *argv[]) {for (int i = 0; i < argc; i++) {printf("Argument %d: %s\n", i, argv[i]);printf("Letters: ");print_letters(argv[i]);printf("Digits: ");print_digits(argv[i]);printf("Punctuation: ");print_punctuation(argv[i]);printf("\n");} }void print_letters(const char *arg) {for (int i = 0; arg[i] != '\0'; i++) {char ch = arg[i];if (isalpha(ch) || isblank(ch)) {printf("'%c' == %d ", ch, ch);}}printf("\n"); }void print_digits(const char *arg) {for (int i = 0; arg[i] != '\0'; i++) {char ch = arg[i];if (isdigit(ch)) {printf("'%c' == %d ", ch, ch);}}printf("\n"); }void print_punctuation(const char *arg) {for (int i = 0; arg[i] != '\0'; i++) {char ch = arg[i];if (ispunct(ch)) {printf("'%c' == %d ", ch, ch);}}printf("\n"); }int main(int argc, char *argv[]) {print_arguments(argc, argv);return 0; } code>
<code class="language-cs">int sum(a, b) int a; // 参数类型声明 int b; // 参数类型声明 {return a + b; } code>
<code class="language-cs">#include <stdio.h>int main() {char arr_char[] = "Hello"; int arr_int[] = {10, 20, 30, 40}; char *p_char = arr_char;int *p_int = arr_int;// 输出原始地址printf("Original char pointer address: %p\n", p_char);printf("Original int pointer address: %p\n", p_int);// 输出加 1 后的地址printf("char pointer after p_char + 1: %p\n", p_char + 1);//偏移量1printf("int pointer after p_int + 1: %p\n", p_int + 1);//偏移量4return 0; } /*输出 Original char pointer address: 0x7ffeeef25a50 Original int pointer address: 0x7ffeeef25a60 char pointer after p_char + 1: 0x7ffeeef25a51 int pointer after p_int + 1: 0x7ffeeef25a64 */code>
<code class="language-cs">int x = 5; int *p1 = &x; // p1 是一级指针c;存储 x 的地址 int **p2 = &p1; // p2 是二级指针c;存储 p1 的地址 /*内存表示 x = 5 p1 -> 地址1 (指向 x) p2 -> 地址2 (指向 p1) */code>
当我们解引用 <code>p2code> 时c;首先得到 <code>p1code> 的值(即 <code>&xcode>)c;然后通过 <code>p1code> 访问 <code>xcode>。
再次解读一个非常好的例子:
<code class="language-cs">#include <stdio.h>int main(){int int_var = 16909060; // 0x01020304 in memory//16909060 对应的十六进制是 0x01020304。在小端模式内存中c;int_var 会以4个字节存储这个值(0x04 0x03 0x02 0x01c;从低地址到高地址)char *char_ptr = (char *)&int_var;printf("char_ptr[0]: %d\n", char_ptr[0]); // 输出最低字节printf("char_ptr[1]: %d\n", char_ptr[1]); // 输出次低字节/*上述的char_ptr[0]其实就是是指针数组的索引操作c;实际上是访问char_ptr指向数组的第二个元素而char_ptr指向int_var的指针c;而int_var被强制转化为char *型指针(偏移量为1)即int_var在内存中其实相当于四个元素的数组c;第一个元素为0x04c;第二个为0x03...char_ptr[0]也相当于对char_ptr的解引用:*char_ptr,故char_ptr[1]等价于*(char_ptr+1)*/return 0; } code>
根据以上分析可以推断出输出结果为:
<code class="language-cs">char_ptr[0]: 4 char_ptr[1]: 3code>
现在我打算向你提供一个词库c;用于读写指针。当你遇到复杂的指针语句时c;试着参考它并且逐字拆分语句(或者不要使用这个语句c;因为有可能并不好):
<code>typecode>类型的指针c;名为<code>ptrcode>。
(<code>ptrcode>所指向位置加上<code>icode>)的值。
注:以字节为单位的话c;应该是<code>ptrcode>所指向的位置再加上<code>sizeof(type) * icode>。
名为<code>ptrcode>c;<code>typecode>类型的指针c;值设置为<code>thingcode>的地址。
<code class="language-cs">// 打印指针所指向的地址 void print_addresses(int *ages, char **names, int count) {int i = 0;while (i < count) {printf("Address of names[%d]: %p, Address of ages[%d]: %p\n", i, &names[i], i, &ages[i]);i++;} }code>
<code class="language-cs">#include <stdio.h>// 函数声明:打印年龄和名字 void print_using_pointers(int *ages, char **names, int count); void print_using_arrays(int *ages, char **names, int count); void print_using_pointers_from_argv(char **argv, int argc); void print_addresses(int *ages, char **names, int count);int main(int argc, char *argv[]) {// 创建数组int ages[] = {23, 43, 12, 89, 2};char *names[] = {"Alan", "Frank", "Mary", "John", "Lisa"};// 获取数组的元素个数int count = sizeof(ages) / sizeof(int);// 使用指针的方式打印print_using_pointers(ages, names, count);printf("---\n");// 使用数组的方式打印print_using_arrays(ages, names, count);printf("---\n");// 使用指针处理命令行参数print_using_pointers_from_argv(argv, argc);printf("---\n");// 打印指针所指向的地址print_addresses(ages, names, count);return 0; }// 使用指针方式打印 void print_using_pointers(int *ages, char **names, int count) {int i = 0;while (i < count) {printf("%s is %d years old.\n", *(names + i), *(ages + i));i++;} }// 使用数组方式打印 void print_using_arrays(int *ages, char **names, int count) {int i = 0;while (i < count) {printf("%s has %d years alive.\n", names[i], ages[i]);i++;} }// 使用指针处理命令行参数 void print_using_pointers_from_argv(char **argv, int argc) {int i = 0;while (i < argc) {printf("Argument %d: %s\n", i, *(argv + i));i++;} }// 打印指针所指向的地址 void print_addresses(int *ages, char **names, int count) {int i = 0;while (i < count) {printf("Address of names[%d]: %p, Address of ages[%d]: %p\n", i, (void*)&names[i], i, (void*)&ages[i]);i++;} } code>