1
|
int
*pointer;
|
1
|
*pointer =
100
; ===>左右类型不一致,赋值非法
|
1
2
3
|
1
.正确指向本程序的变量 ===>正确
2
.指向其它程序未授权的地址 ===>非法
3
.未赋初值,只有随机值,指向未知地方 ===>非法
|
1
2
3
4
5
6
7
8
9
|
#
include
<stdio.h>
void
swap(
int
*p1,
int
*p2)
{
int
*p3; ===>定义p3指针变量时已有初值,为随机值
*p3 = *p1; ===>修改p3指向的值,会出现“未授权”的情况,即程序试图去修改一个未知地方的值
*p1 = *p2; 这显然是会引起错误的。
*p2 = *p3;
}
|
1
2
|
& 取地址运算符,&a取变量a的地址
* 指针运算符,*p取指针p所指向的变量的值
|
1
|
pointer = &a ===>把a的地址赋给指针变量,或让pointer指向a
|
1
|
printf(
"%d"
,*pointer); ===>输出a的值
|
1
|
printf(
"%d"
,pointer); ===>直接输出指针变量的值,是一个内存地址
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#
include
<stdio.h>
int
main(
void
)
{
int
a,b;
int
*p1,*p2;
printf(
"Please input a:"
);scanf(
"%d"
,&a);
printf(
"Please input b:"
);scanf(
"%d"
,&b);
printf(
"a=%d, b=%d\n"
,a,b);
p1 = &a;p2 = &b;
swap(p1,p2);
printf(
"Now, a=%d, b=%d\n"
,a,b);
return
0
;
}
|
1
2
3
4
5
6
7
8
9
|
#
include
<stdio.h>
void
swap(
int
*p1,
int
*p2)
{
int
temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
|
1
2
3
4
5
6
|
xpleaf@leaf:~/stuc$ gcc -o swap swap_main.o swap_sec.o
xpleaf@leaf:~/stuc$ ./swap
Please input a:
3
Please input b:
4
a=
3
, b=
4
Now, a=
4
, b=
3
|
1
2
3
|
p1+
1
:表示指向下
1
个元素,指针移动基类型个字节
p1-
1
:表示指向上
1
个元素,指针移动基类型个字节
p1+
2
:表示指向下
1
个元素,指针移动(基类型 *
2
)个字节
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#
include
<stdio.h>
int
main(
void
)
{
int
a[
10
], *p, i;
for
(i =
0
;i <
10
;i++)
a[i] = i +
10
;
p = a;
for
(i =
0
;i <
10
; p++, i++)
printf(
"a[%d]=%d "
, i, *p);
printf(
"\n"
);
return
0
;
}
|
1
2
3
4
|
xpleaf@leaf:~/stuc/shuzu$ gcc -c value.c
xpleaf@leaf:~/stuc/shuzu$ gcc -o value value.o
xpleaf@leaf:~/stuc/shuzu$ ./value
a[
0
]=
10
a[
1
]=
11
a[
2
]=
12
a[
3
]=
13
a[
4
]=
14
a[
5
]=
15
a[
6
]=
16
a[
7
]=
17
a[
8
]=
18
a[
9
]=
19
|
1
2
3
|
void
swap(
int
a[])
等价于
void
swap(
int
*a)
|
1
|
swap(a) ===>实际放入的都是指针
|
1
|
a[
5
][
2
] = {{
1
,
2
}, {
3
,
4
}, {
5
,
6
}, {
7
,
8
}, {
9
,
10
}};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#
include
<stdio.h>
int
main(
void
)
{
int
a[
5
][
2
] = {
{
1
,
2
},
{
3
,
4
},
{
5
,
6
},
{
7
,
8
},
{
9
,
10
}
};
int
(*p)[
2
], i, j;
p = a;
for
(i =
0
;i <
5
; i++)
for
(j =
0
;j <
2
;j++)
printf(
"a[%d][%d]=%d\n"
, i, j,*(*(p+i)+j));
return
0
;
}
|
1
2
3
4
5
6
7
8
9
10
11
|
xpleaf@leaf:~/stuc/shuzu2$ ./value2
a[
0
][
0
]=
1
a[
0
][
1
]=
2
a[
1
][
0
]=
3
a[
1
][
1
]=
4
a[
2
][
0
]=
5
a[
2
][
1
]=
6
a[
3
][
0
]=
7
a[
3
][
1
]=
8
a[
4
][
0
]=
9
a[
4
][
1
]=
10
|
1
|
printf(
"print %d\n"
, *(*p+
3
));
|
1
|
print
4
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#
include
<stdio.h>
int
main(
void
)
{
int
a[
5
][
2
] = {
{
1
,
2
},
{
3
,
4
},
{
5
,
6
},
{
7
,
8
},
{
9
,
10
}
};
int
(*p)[
2
], i,j;
p = a;
fun(p,
5
);
return
0
;
}
|
1
2
3
4
5
6
7
8
9
|
#
include
<stdio.h>
void
fun(
int
(*p)[
2
],
int
n)
{
int
i, j;
for
(i =
0
;i < n;i++)
for
(j =
0
;j <
2
;j++)
printf(
"a[%d][%d]=%d\n"
,i, i, *(*(p+i)+j));
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
xpleaf@leaf:~/stuc/shuzu3$ gcc -o value3 value3.o value3_fun.o
xpleaf@leaf:~/stuc/shuzu3$ ./value3
a[
0
][
0
]=
1
a[
0
][
0
]=
2
a[
1
][
1
]=
3
a[
1
][
1
]=
4
a[
2
][
2
]=
5
a[
2
][
2
]=
6
a[
3
][
3
]=
7
a[
3
][
3
]=
8
a[
4
][
4
]=
9
a[
4
][
4
]=
10
|
1
2
3
4
5
6
7
|
#
include
<stdio.h>
int
main(
void
)
{
char string[] =
"I hope CL can be my girlfriend."
;
printf(
"%s\n"
,string);
return
0
;
}
|
1
2
3
4
|
xpleaf@leaf:~/stuc/str$ gcc -c str1.c
xpleaf@leaf:~/stuc/str$ gcc -o str1 str1.o
xpleaf@leaf:~/stuc/str$ ./str1
I hope CL can be my girlfriend.
|
1
2
3
4
5
6
7
|
#
include
<stdio.h>
int
main(
void
)
{
char *string =
"I hope CL can be my girlfriend."
;
printf(
"%s\n"
,string);
return
0
;
}
|
1
2
3
4
5
6
|
*string =
"I hope CL can be my girlfriend."
; ===>直接定义初始化,让string指向字符串首地址
等价于
char *string; ===>先定义char指针类型
string =
"I hope CL can be my girlfriend."
===>让string指向字符串首地址
等价于
char string[] =
"I hope CL can be my girlfriend."
; ===>通过数组方式实现
|
1
2
|
char str[
14
];
str =
"I hope CL can be my girlfriend."
; ===>非法赋值
|
1
|
char str[
14
] =
"I hope CL can be my girlfriend."
;
|
1
2
3
4
|
char a[] =
"I hope CL can be my girlfriend."
;
char *b =
"I hope CL can be my girlfriend."
;
a[
2
] =
'I'
; ===>合法
b[
2
] =
'I'
===>非法
|