C 指针
来源:互联网 发布:淘宝注册怎么注册账号 编辑:程序博客网 时间:2024/06/17 07:36
主要内容
- 指针
- 函数指针
- 全局变量和函数返回值
指针
- 什么是指针?
- 指针就是变量的地址
&取地址操作符
&myVar;
如何声明指针变量
int *mypointer; 或者 int* myPointer;(不推荐)int表示指针内存放的数据类型,*表示声明的是指针,myPointer指针变量的名字
如何使用指针
int *myPointer;int myVar;myPointer = &myVar;*myPointer = 27; myVar的值是多少呢?为什么? 因为指针变量myPointer的地址指向myVar的存放地址,所以当给myPointer地址内存放27时, myVar的存放地址内的值也是27,即myVar = 27.
函数参数
什么是函数参数
- 函数的参数是用于在函数调用的地方与被调用函数之间共享变量。比如说
我们要实现一个加法功能的函数addTwos(),addTwos()使用两个数值,然后将数值相加,返回它们的和。但问题是 1,函数如何获取原始的两个数据的值? 2,函数调用繁荣地方如何得到addTwos()计算的和? 答案就是使用函数参数!
- 函数的参数是用于在函数调用的地方与被调用函数之间共享变量。比如说
变量的作用域
在C语言中每一个变量都有作用域。当变量在程序的某个部分被声明时,它只有在程序的一定区域才能被访问,这个区域就是变量的作用域。总体来说,在C语言中编译器可以确认4种不同类型的作用域:
- 文件作用域
- 函数作用域
- 代码块作用域
- 原型作用域
函数作用域
- 函数作用域指的是:变量在函数中声明,那么此变量是
这个函数的局部变量,只能在这个函数内被访问和使用。换 句话说,你不能在一个函数中声明一个变量,然后在另外一 个函数中来引用。例如:
ERROR:#include <stdio.h> int main (int argc, const char * argv[]) { int numDots; numDots = 500; DrawDots(); return 0; }void DrawDots( void ) {int i;for ( i = 1; i <= numDots; i++ ) printf( "." );}
- 函数作用域指的是:变量在函数中声明,那么此变量是
函数参数如何工作
函数参数与变量类似,所不一样的是函数的参数在参数列表里声明,即:函数声明中两个圆括号之 间。例如:
#include <stdio.h>void DrawDots( int numDots ); //numDots函数参数int main (int argc, const char * argv[]) { DrawDots( 30 ); return 0;}void DrawDots( int numDots ) { int i; for ( i = 1; i <= numDots; i++ ) printf( "." );}函数参数都是临时参数,当函数调用的时候,临时变量被创建,当函数退出后,变量销毁。
行参和实参
传给函数的实际值叫做实参;函数定义中参数列表用于接收实参的参数叫做形参。 void DrawDots( int numDots ); //numDots是形参 DrawDots( 30 ); //30是实参
指针在函数中的作用
#include <stdio.h>void ChangeVarValue( int numDots ); int main (int argc, const char * argv[]) { int numDots; numDots = 30; ChangeVarValue( numDots ); printf( "in main numDots’s value: %d\n”,numDots ); return 0; }void ChangeVarValue( int numDots ) { numDots = 40; printf( "numDots’s value is %d\n”,numDots );}
#include <stdio.h>void ChangeVarValue( int *numDots ); int main (int argc, const char * argv[]) { int numDots; numDots = 30; ChangeVarValue( &numDots ); printf( “in main numDots’s value: %d\n”,numDots ); return 0; }void ChangeVarValue( int *numDots ) { *numDots = 40; printf( “numDots’s value is %d\n”,*numDots );}
全局变量和函数返回值
- 全局变量和函数返回值是另外两种在不同的函数中共享数据的机制
全局变量顾名思义是指在整个源文件内,所有函数都可以访问,修改的变量
#include <stdio.h>void ChangeVarValue( int *numDots ); int main (int argc, const char * argv[]) { int numDots; numDots = 30; ChangeVarValue( &numDots ); printf( “in main numDots’s value: %d\n”,numDots ); return 0; }void ChangeVarValue( int *numDots ) { *numDots = 40; printf( “numDots’s value is %d\n”,*numDots );}
#include <stdio.h>void PassAlong( void );void PrintMyVar( void );int gMyVar;//全局变量的声明方法注意变量前的gint main (int argc, const char * argv[]) { gMyVar = 10; PassAlong();return 0;}void PassAlong( void ) { PrintMyVar();}void PrintMyVar( void ) { printf( "gMyVar = %d", gMyVar );}
函数返回值
通过函数返回值实现不同函数之间的数据共享。#include <stdio.h>int AddTheseNumbers( int num1, int num2 );int main (int argc, const char * argv[]) { int sum; sum = AddTheseNumbers( 5, 6 ); printf( "The sum is %d.", sum );return 0; }int AddTheseNumbers( int num1, int num2 ) {return ( num1 + num2 );}对于函数返回值为空的函数,可以使用return退出函数
0 0
- C指针的指针
- c 指针的指针
- C--指针
- C-指针
- C指针
- C指针
- C指针
- C---指针
- c 指针
- C指针
- C指针
- C指针
- C 指针
- c指针
- C指针
- C 指针
- C 指针
- C 指针
- z-index属性
- mysql常用的东西
- ViewGroup的OnInterceptTouchEvent方法的调用特点
- 22栈的压入,弹出序列
- 在windows上编译scite
- C 指针
- C语言 用for语句老实现打印九九乘法口诀表
- poj 1681
- C++常用类 shared_ptr
- 凸优化:ADMM(Alternating Direction Method of Multipliers)交替方向乘子算法系列之二:Precursors
- 大型互联网架构概述
- 使用MYSQL benchmark - DBT2 Benchmark Tool遇到的问题
- 如何获取系统当前时间
- iOS数据存储