作用域 static extern malloc relloc
windows里命令行参数
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
只有可执行程序的时候,可以把文件拖拽到可执行程序上面
相当于 把文件名当参数传递了
变量作用域:
#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;}

结果是正确的,但是极其不建议这样使用
register寄存器变量 不能取其地址
- #include <stdio.h>
#include <string.h>int main(){//int i = 10;register int i = 10;printf("%p\n",&i); //编译就不通过return 0;}
static 静态变量:
static虽然在块作用域内,但在程序刚加载的时候就存在了,并且只初始化了一次,在内存中位于静态区。
不过块作用域外面还是无法访问到
int main(){while( i-- ){static int i = 0;}return 0;}
两个不同文件之间, static用来限制变量只在当前文件有效:
a.c
int a = 5;int main(){printf("%d\n" , a);return 0;}
b.c
int a = 10;
编译报错,`a'被多次定义
解决方法在a.c文件里 int前面加个static
访问的就是当前这个文件的a,即输出5

如果两个文件都有static,说明两个文件内都有a这个变量
a.c
#include <stdio.h>static int a = 5;int main(){printf("%d\n" , a);return 0;}
b.c
static int a = 10;
结果也是5
两个不同文件之间, extern引用同一个变量:
a.c
#include <stdio.h>int main(){printf("%d\n" , a);return 0;}
b.c
int a = 10;
编译报错,找不到a
解决方法在a.c里全局变量里加入int a 或者 extern int a 或者 extern a 只进行一个声明
#include <stdio.h>extern int a;int main(){printf("%d\n" , a);return 0;}
或者在a.c局部变量里 extern int a 或者 extern a 注意 这里直接int a 不行 ,就相当于定义了一个未初始化的局部变量;
#include <stdio.h>int main(){extern int a;printf("%d\n" , a);return 0;}

两个不同文件之间 函数声明引用同一个函数:
下面可以正确执行,但是建议在a.c里 声明一下 func ,就是函数的原型
a.c
#include <stdio.h>void func(); //省略一样正确执行int main(){func();return 0;}
b.c
#include <stdio.h>void func(){printf("func\n");}

如果在b.c文件里的func函数前面加个static,那么就会访问不到这个函数
因此可以得出结论
不同文件之间 使用同一个函数 需要函数声明
不同文件之间 使用同一个变量 需要extern
全局变量和静态变量:
注意,全局变量和静态变量,不初始化的时候,系统会自动初始化为0
#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;}

free一个函数返回的指针,因为地址一样:
#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;}
这个严重新手级的错误,你传递的指针只是传递的当前的NULL,新开辟的内存返回地址,你不知道

解决方案有三种:
第一种解决方案是把传递指针的地址 上策
#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;}

第三种解决方案是在main函数里开辟 下策
#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;}

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

sizeof(p) p是个纯指针,不是数组名, 两者sizeof有区别
解决方法:
memset(p, 0, sizeof(int) * 5);

realloc的应用:动态添加字符串
#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;}
先malloc得到一块新的内存地址,都是垃圾值

init_str 初始化 先把所有内存清零

然后strcpy hello

realloc 申请内存,不过新申请的内存还是垃圾值。

通过memset把realloc申请的新内存 清零

通过strcat 连接两个字符串


realloc新增加的的不会清0,需要自己清理
解决方法:
memset( p+strlen(p) , 0 , strlen(s)+1);
realloc缩小内存:
- #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);}



可以得出realloc缩小内存的情况,原来的值都会变成垃圾值。如果需要输出正常,要在最后修改成\0
realloc的其他用法:
char *p = realloc ( NULL , 5 ); 等于 char *p = malloc( 5 );

浙公网安备 33010602011771号