转载 让指针不再困扰你

声明:moonwalker 作于2008年4月26日 转载请注明出处 指针对一部分初学者来说一直是一个无法逾越的障碍,没有指针的C语言就好像没有左腿的短跑运动员。今天我来试试换一种方法来理解指针,希望能帮还在为指针挣扎的朋友们理清思路,高手略过即可。 我们先认为内存是一家客栈(看起来这和老掉牙的大楼的比喻没什么区别,但是请你耐心看下去)。同大部分的客栈一样,这家客栈有天、地、人等各种档次的房间,用现在的话就是单人间、双人间、四人间等等。来住店的客人也是车水马龙,三教九流都有,我们看看都有哪些人: 快乐单身汉char,还有char的小弟unsigned char,他们只住...
文章 | 2009-02-17 14:48 | 阅读 2887 次 | 评论 1 条

最简单的指针

将输入的字符串的最后一位变成x,如 input abcd output abcx --------------- #include <stdio.h> int main(void) { char a[10], *p; p = a; scanf("%s",p); for ( ; *p !='\0' ; p++) ; p--; *p = 'X'; printf("%s",a); getch(); return 0; }
其他 | 2009-02-17 14:46 | 阅读 1746 次 | 评论 1 条

1.5.1 Word Counting(The wrong version)

#include <stdio.h> #define OUT 0 /* outside word */ #define IN 1 /* inside word */ int main(void) { int c, nw; nw = 0; while((c = getchar()) != EOF) { while ((c >= 'a' &amp;&amp; c <= 'z') || (c >= 'A' &amp;&amp; c <= 'Z')) ; +...

求百位 十位 个位数 都不相同的3位数都列出来

#include <stdio.h> int main(void){ int num, baiwei, shiwei, gewei, i, j; i = j = 0; for (num = 100; num < 999 ; ++num) { baiwei = num / 100; shiwei = (num - baiwei*100) / 10; gewei = num - baiwei*100 - shiwei *10; if ((baiwei != shiwei) &amp;&amp; (ba...
其他 | 2009-02-13 12:36 | 阅读 2271 次 | 评论 0 条

Exercise 1-10

/* Write a program to copy its input to its output,replacing each tab by \t, *each backspace by \b,and each backslash by \\ .This makes tabs and backspaces *visible in an unambiguous way */ #include <stdio.h> int main(void) { int c; while ((c = getchar()) != EOF) { if...

Exercise 1-9

/*Write a program to copy its input to its output, *replacing each string of one or more blanks by a single blank*/#include <stdio.h> #define OUT 1 /* outside the word */#define IN 0 /* inside the word */ int main(void){ int c,state; state = OUT; while ((c = getchar()) != EO...

Exercise 1-8

/* Write a program to count blanks,tabs,and newlines */ #include <stdio.h> int main(void) { int c, nb, nt, nn; nb = nt = nn = 0; while((c = getchar()) != EOF) { if (c == ' ') ++nb; if (c == '\t') ++nt; if (c == '\n') ++nn; } print...

1.5.3 Line Counting

/* 1st version */ #include <stdio.h> int main(void) { int c, nl; nl = 0; while ((c = getchar()) != EOF) if (c == '\n') ++nl; printf("%d",nl); getch(); return 0; } =========================== /* 2nd version */ #include <stdio.h...

1.5.2 Character Counting

/* 1st version */ #include<stdio.h> int main(void) { int c, nc; nc = 0; while((c = getchar()) !=EOF) { ++nc; } printf("%d",nc); getch(); return 0; } ====================== /* 2nd version */ #include <stdio.h> int main(v...

Exercise 1-7

/* Write a program to print the value of EOF */ /* 1st version */ #include <stdio.h> int main(void){ int c; c = EOF; printf("the value of EOF is %d",c); getch(); return 0;} ===============/* 2nd version */ #include <stdio.h> int main(void){ printf("...

1.5.1 File Copying

/* 1st verion.copy input to output */ #include <stdio.h> int main (void){ int c; c = getchar(); while (c != EOF) { putchar(c); c = getchar(); } return 0;} =========================== /* 2ed version.copy input to output */ #include <stdio.h> ...

1.4 Symbolic Constants

/* The version of symbolic constants : print Fahrenheit-celsious table */ #include<stdio.h> #define LOWER 0#define UPPER 300#define STEP 20 int main(viod){ float fahr; for (fahr = LOWER; fahr <= UPPER; fahr += STEP ) { printf("%3.0f%6.1f\n", fahr, (5.0/9.0) * ( f...

Exercise 1-5

/* Modify the temperature conversion program to print the table in reverse order, *that is form 300 degrees to 0. */ #include <stdio.h> int main(void){ float fahr; for(fahr = 300; fahr >= 0; fahr-=20) { printf("%3.0f%6.1f\n",fahr,(5.0/9.0) * (fahr - 32)); } ...

1.3 The for statement

/* the version of for statement;print Fahrenheit - Celsius table */ #include <stdio.h> int main(void){ float fahr; for ( fahr = 0; fahr <=300; fahr+=20) { printf("%3.0f%6.1f\n",fahr,(5.0/9.0)*(fahr-32)); } getch(); return 0;}

Exercise 1-4

/* Write a program to print the corresponding Celsius to Fahrenhei table */ #include <stdio.h> int main(void){ float cels, fahr; int lower, upper, step; lower = 0; upper = 100; step = 10; cels = lower; while ( cels <= upper) { fahr = 9 * cels / 5 + 3...

Exercise 1-3

/* Modify the temperature conversion program to print a heading above the table */ #include <stdio.h> int main(void){ float fahr, cels; int lower, upper, step; lower = 0 ; upper = 300; step = 20; fahr = lower; printf("F\tC\n"); while (fahr <= upper) { ...

1.2 Variables and Arithmetic Expressions

The program uses the formula [ Celsius = (5/9) * (Fahrenheit - 32)] to print the following table of Fahrenheit temperatures and their centigrade or Celsius equivalents: ============= /* print Fahrenheit - Celsius table for fahr =0, 20, ..., 300 */ #include <stdio.h> int main(void) { ...

1.1 Getting Started

the program to print "hello, world" ============== #include <stdio.h> int main(void) { printf("hello, world"); return 0; }
浏览27695次
文章归档