#include <stdio.h>#include <string.h>int main(){int i = 10;while( i-- ) //这个i是上面的i,所以只会输出10次{int i = 0;//去掉这一句,结果会变成一个死循环,会无限输出9printf("i = %d\n" , i++);}return 0;}

- #include <stdio.h>
#include <string.h>int main(){int *p = NULL;{int i = 100;p = &i;}*p = 10;printf("*p = %d\n" , *p);return 0;}

- #include <stdio.h>
#include <string.h>int main(){//int i = 10;register int i = 10;printf("%p\n",&i); //编译就不通过return 0;}
int main(){while( i-- ){static int i = 0;}return 0;}
int a = 5;int main(){printf("%d\n" , a);return 0;}
int a = 10;

#include <stdio.h>static int a = 5;int main(){printf("%d\n" , a);return 0;}
static int a = 10;
#include <stdio.h>int main(){printf("%d\n" , a);return 0;}
int a = 10;
#include <stdio.h>extern int a;int main(){printf("%d\n" , a);return 0;}
#include <stdio.h>int main(){extern int a;printf("%d\n" , a);return 0;}

#include <stdio.h>void func(); //省略一样正确执行int main(){func();return 0;}
#include <stdio.h>void func(){printf("func\n");}

#include <stdio.h>int a;int main(){printf("%d\n" , a);return 0;}

#include <stdio.h>void test(int a , int b){ //注意,参数是在这个位置才开始入栈,不是在main函数调用时入的printf("%p , %p\n" , &a , &b); //注意,函数形参也是在栈区,形参是从右到左入栈,所以右面的参数地址高于左边的参数地址}int main(){int a = 10 ;int b = 20;printf("%p , %p\n" , &a , &b);test( a , b);return 0;}

#include <stdio.h>#include <stdlib.h>#include <string.h>char *test(){char *s = malloc(sizeof(char) * 10);printf("s = %p\n" , s);strcpy( s , "hello");return s;}int main(){char *p = test();printf("%s\n",p);printf("p = %p\n" , p);free(p); //这种情况完全是可以free的,因为返回的地址和你要释放的地址是一个地方return 0;}

#include <stdio.h>#include <stdlib.h>#include <string.h>void test( char * p){p = malloc(sizeof(char) * 10);strcpy( p , "hello");printf("%p\n",p);}int main(){char *s = NULL;test(s);printf("%p\n",s);printf("%s\n" , s);return 0;}

#include <stdio.h>#include <stdlib.h>#include <string.h>void test( char ** p){*p = malloc(sizeof(char) * 10);strcpy( *p , "hello");printf("%p\n",*p);}int main(){char *s = NULL;test(&s);printf("%p\n",s);printf("%s\n" , s);free(s);return 0;}

#include <stdio.h>#include <stdlib.h>#include <string.h>char * test( char * p){p = malloc(sizeof(char) * 10);strcpy( p , "hello");printf("%p\n",p);return p;}int main(){char *s = NULL;s = test(s);printf("%p\n",s);printf("%s\n" , s);free( s );return 0;}

#include <stdio.h>#include <stdlib.h>#include <string.h>void test( char * p){strcpy( p , "hello");printf("%p\n",p);}int main(){char *s = NULL;s = malloc(sizeof(char) * 10);test(s);printf("%p\n",s);printf("%s\n" , s);free(s);return 0;}

#include <stdio.h>#include <string.h>int main(){int *p = malloc(sizeof(int) * 5);memset(p, 0, sizeof(p)); //注意这里只把4个字节的内存清零了}


#include <stdio.h>#include <stdlib.h>#include <string.h>void init_str( char **p ){*p = malloc( sizeof(char) * 10 );memset( *p , 0 ,sizeof(char) * 10 );strcpy( *p , "hello" );}char* append_str( char *p , const char * s){p = realloc( p , strlen(p) + strlen(s) + 1);memset( p+strlen(p) , 0 , strlen(s)+1); //这是把申请的新内存清零strcat(p,s);return p;}int main(){char *s = NULL;init_str( &s );s = append_str( s , " world");printf("%s\n" , s);return 0;}







- #include <stdio.h>
- #include <string.h>
#include <stdlib.h>int main(){char *p = malloc(sizeof(char) * 20);memset(p, 0, 20);strcpy(p , "123456789");p = realloc(p , 5);printf("%s\n",p);}



9.1 作用域 static extern malloc relloc
原文:http://www.cnblogs.com/l6241425/p/3955052.html