C语言英文课件9:指针

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

i_pointer=&i ;
i_pointer
...
scanf(“%d”,i_pointer);
9 . 2 Pointer variable points to a variable
The definition of pointer variable
general format:
base-type *pointer-variable ; where:base-type is the type of the value to which the pointer points
scanf ("%d%d",&a,&b);
p1=&a; p2=&b;
if (a<b)
if(*p1<*p2)
{p=p1;pl=p2;p2=p;}
printf ("\na=%d,b=%d\n”,a,b);
printf("max=%d,min=%d\n",*pl,*p2);
}
results: 5 9
a=5,b=9
main() { int a,b;
int *pointer_1, *pointer_2; a=100;b=10; pointer_1=&a; /*to assign a’s address to pointer_1*/ pointer_2=&b;/* to assign b’s address to pointer_2*/ printf (”%d,%d\n”, a,b); printf(”%d,%d\n”, *pointer_1,*pointer_2);
to a pointer)
p1
for example, int *p1,*p2, i;
p1=&i;
i
p2=p1;
p2
2. to assign a value to a variable with pointer variable
“*”:pointer operator、indirection operator is an unary operator(value-pointed-to)
to realize two variable exchange?
void swap(int p1,int p2)
{ int t;
t=p1; p1=p2; p2=t;
a
b
}
3
6
main()
{ int a,b;
printf("a,b=");
scanf("%d%d",&a,&b);
3
6
swap(a,b);
/*design a function to sort three numbers*/ void sort(int *p1,int *p2,int *p3) { if( *p1 < *p2 ) swap( p1, p2 );
Mechanism:
When system execute the called function , if the formal parameter pointer variable changes, the value is saved via actual parameter at the end of
max=9,min=5
p1
a
p
&a
5
p2
b
&b
9
p1
a
p
&b
5
p2
b
&a
9
Modify program as follows:
main() { int *p1,*p2,p, a, b;
scanf ("%d%d",&a,&b); p1=&a; p2=&b; if (*p1<*p2)
{p=*p1;*pl=*p2;*p2=p;} printf ("\na=%d,b=%d\n”,a,b); printf("max=%d,min=%d\n",*pl,*p2);
pointer variable
memory user data region
Pointer is address
...
address
1. the address of the memory storage unit ──the serial number of the storage unit
cf.data storing in the memory storage unit
pointer:The elite of C,import concept, import feature. Using pointer: 1.can make program more compact, 2.be care of using pointer
9 . 1 The concept of the pointer and
variable
address
3. The access of variable value
(1)Direct access
scanf(”%d”,&m); printf(”%d”, m);
Memory user data region
...
m=m+i;
(2) indirect access
2000 4 Variable k 2002 2 Variable m
then: &*p1 =&(*p1)=&(a)=&a=p1 *&a =*(&a)=a
(2)priority
* prior to &
3.”--”and “++”operator mostly apply to
arrays’ pointer
if :float x,*p1; p1=&x;
if p=1000,
main() { int a,b;
printf("a,b="); scanf("%d%d",&a,&b); swap(&a,&b); printf("a=%d,b=%d\n",a,b); }
&a
&b
p1
p2
Summary:
We must use pointer variable as function parameter to return the alternative value to calling function from called function
visit variable value through 2004 7 Variable i
another variable (a pointer2006 9 Variable j
variable)
.
Example:
..
define a pointer” i_pointer”
3010 2004 Variable
Chapter 9 Pointer(指针)
9.1 The conception of the pointer and pointer variable
9.2 Pointer variable points to a variable 9.3 Pointer points to an array 9.4 Pointer points to a string 9.5 Function that return a pointer 9.6 Pointer arrays and the formal parameters of the main () 9.7 Pointer points to a function
} The results:
100, 10
pointer1
&a
a(*pointer1)
100
100, 10
pointer2
b (*pointer2)
&b
10
[9.2]Input two integers a and b ,output them ordered by value .
main()
{ int *p1,*p2,*p, a, b;
for example ,int i , j , *p;
p=&i;
*p=3;
namely i=3;
j=*p+2;
After definition After p=&i
:
:
1000
i
1002
j
1004
p
1000
i
1002
j
1004 1000 p
:
:
after *p=3
after j=*p+2
1000 1002 1004
3 1000
i 1000 j 1002 p 1004
3 5 1000
i j p
Pointer variable must be assigned a value before it is
operator & and *:
(1)associativity :from right to left
if:int a, *p1; p1=&a;
Байду номын сангаас
2000 0000 0000 2001 0000 0100 2002 0000 0000 2003 0000 0111
. ..
3010 00000000 3011 11001100
...
2. Variable address and value
(1)variable address── The address of a variable is itself data that can be manipulated and stored in memory
...
How does the compiler process variable definition ? main()
{ int num; scanf("%d",&num); printf("num=%d\n", num);
}
C compiler does operate as follows when it compile variable definition statement ” int num”:
then p++ is 1004
example: int a,b,*p;
p=&a; a=3; b=5;
(*p)++; the equivalence is a++ a==4
*p++; the equivalence is *(p++)
if p=1000,
then p++ is 1002
[9.1]The indirect access of variables
the function call.
[9.4] Input three integer ,then output them in descending order that realized by using pointer as real parameter.
void swap(int *p1, int *p2) { int t; t=*p1, *p1=*p2, *p2=t; }
printf("a=%d,b=%d\n",a,b);} p1
p2
[9.3] pointer variable act as function
parameter void swap(int *p1,int *p2) { int t;
a
b
3
6
t=*p1; *p1=*p2; *p2=t;
}
Transfer value(address)
(1) distribute variable memory space (2) login variable num to "symbol table" each variable in symbol table contains two attribute: 1) variable name(id) 2) its address (addr)
(2)value——can be stored in memory
(3)lvalue
memory user data region
...
variable k
2000
4
2002
2 variable m
2004 7 variable i
2006
9 variable j
...
3010 2004 variable i_pointer
}
question:1.analyses function result
5 9 a=?,b=? max=?,min=?
2.how about add three *before p?
4.pointer variable act as function parameter
question:can the swap function able
pointer-variable is the variable being declared
for examples, int *p1; char *p2;
using a pointer variable
1. assignment of a pointer variable value ( to assign an address
相关文档
最新文档