【本文链接】

http://www.cnblogs.com/hellogiser/p/pointer-summary.html

1.指针注意事项

(1). 指针类型字符串不容许修改

char *str1=”abcd”; char str2[]=”abcd”;的区别。指针类型的字符串一般不允许修改,如:str1[0]=’c’;这样的语句会导致运行时错误。

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
void test_string()
{
    char *str1 = "abcd";
    char str2[] = "1234";

cout << str1 << endl; // abcd
    cout << str2 << endl; // 1234
    cout << *str1 << endl; // a
    cout << *str2 << endl; // 1

//str1[0]='X'; // ERROR
] = 'Y'; // right
    cout << *str2 << endl; // Y
}

(2).指针/数组作为参数进行传递

在C语言中,参数是使用值传递的。 int func(int a );当调用者调用该函数的时候将传递一个值给a,这个a只是你传递进去的参数的一个副本。而数组传递的时候,会退化为指针,其将数组的首地址复制给形参。看下面的一个例子。

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/6/3
*/
void fun(char str[])
{
    printf("After transform:%d\n", sizeof(str));  //4
}
int main()
{
    char strs[] = "abcdefg";
    printf("Before transform:%d\n", sizeof(strs)); //8
    fun(strs);
    ;
}

(3). 在函数内部修改指针本身(无效)vs修改指针指向的内容(OK)

许多初学C指针的同学想在函数内部修改作为参数传递进来的指针的值。看以下代码:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/6/3
*/
#include "stdafx.h"
#include <iostream>
using namespace std;

typedef struct Link_Node
{
    int Elem;
    struct Link_Node *next;
} LinkNode, *PLinkList;

void CreateList(LinkNode *header)
{
    ;
    header = (LinkNode *)malloc(sizeof(LinkNode));
    header->Elem = ;
    header->next = NULL;
}

int main()
{
    LinkNode *head = NULL;
    CreateList(head);
    if(head != NULL)
        printf("%d\n", head->Elem);
    free(head);
    ;
}

/*
A: head---0
---CreateList---
B: header---0
C: NODE
B: header---C
---CreateList---
A: head---0
*/

此做法无效,在函数内部修改header的地址,函数结束后回到main函数,head仍然是null。

我们可以修改其指向的变量的值,而不能修改指针本身的内容了。为了能够修改指针本身的内容,我们需要传递指针本身的地址。所以在上面那例中,需要传递head指针本身的地址。代码如下:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/6/3
*/
#include "stdafx.h"
#include <iostream>
using namespace std;

typedef struct Link_Node
{
    int Elem;
    struct Link_Node *next;
} LinkNode, *PLinkList;

void CreateList(LinkNode **header)
{
    ;
    (*header) = (LinkNode *)malloc(sizeof(LinkNode));
    (*header)->Elem = ;
    (*header)->next = NULL;
}

int main()
{
    LinkNode *head = NULL;
    CreateList(&head);
    if(head != NULL)
        printf("%d\n", head->Elem);
    free(head);
    ;
}

/*
A: head---0
---CreateList---
B: header---A
C: NODE
A: head---C
B: header---A
---CreateList---
A: head---C
*/

下面我们再看一例,在函数内部修改指针指向的内容。

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/6/3
*/
#include "stdafx.h"
#include <iostream>
using namespace std;
void Trans(int *Arr, int nLength)
{
    ; i < nLength; i++)
    {
        // modify content of *pointer
;
    }
}

int main()
{
    };
    int  i;
    printf("Before:\n");
    ; i++)
        printf("%d  ", nArr[i]);
    //0 0  0  0  0

Trans(nArr, );
    printf("\nAfter\n");
    ; i++)
        printf("%d  ", nArr[i]);
    // 20  21  22  23  24
;
}

http://blog.csdn.net/gamecreating/article/details/5382711

http://www.cnblogs.com/nezha/p/3204410.html

2. 指针vs数组

数组名不是变量!指针是变量!

数组名仅仅是一个符号,不是变量,它没有自己的存储空间,而指针是个变量,有自己的空间。

C标准有一条:当数组出现在函数的参数中时,无论是形参还是实参,都转换为指针再进行操作。

http://blog.jobbole.com/44863/

3. 指针的指针

http://blog.jobbole.com/60647/

4. 数组指针vs指针数组

http://blog.csdn.net/touch_2011/article/details/6966980

http://www.cnblogs.com/hongcha717/archive/2010/10/24/1859780.html

简单举例说明:

int *p[2]; 首先声明了一个数组,数组的元素是int型的指针。

int (*p)[2]; 声明了一个指针, 指向了一个有两个int元素的数组。(数组指针,也称行指针)

其实这两种写法主要是因为运算符的优先级, 因为[]的优先级比*高。所以第一种写法,p先和[]结合,所以是一个数组,后与*结合,是指针。后一种写法同理。

指针数组如下处理就会很清楚: typedef int* intPtr; intPtr p[2]; 一目了然,所以为了避免迷惑,做适当的typedef也是很有必要的。

同理,数组指针也可以作类似处理: typedef int intArray2[2]; intArray2 * p; 和原来的声明都是等价的。

数组指针(也称行指针)

定义 int (*p)[n];

()优先级高,首先说明p是一个指针,指向一个整型的一维数组,这个一维数组的长度是n,也可以说是p的步长。也就是说执行p+1时,p要跨过n个整型数据的长度。

如要将二维数组赋给一指针,应这样赋值:

int a[3][4];

int (*p)[4]; //该语句是定义一个数组指针,指向含4个元素的一维数组。

p=a;        //将该二维数组的首地址赋给p,也就是a[0]或&a[0][0]

p++;       //该语句执行过后,也就是p=p+1;p跨过行a[0][]指向了行a[1][]

所以数组指针也称指向一维数组的指针,亦称行指针。

指针数组

定义 int *p[n];

[]优先级高,先与p结合成为一个数组,再由int*说明这是一个整型指针数组,它有n个指针类型的数组元素。这里执行p+1是错误的,这样赋值也是错误的:p=a;因为p是个不可知的表示,只存在p[0]、p[1]、p[2]...p[n-1],而且它们分别是指针变量可以用来存放变量地址。但可以这样 *p=a; 这里*p表示指针数组第一个元素的值,a的首地址的值。

如要将二维数组赋给一指针数组:

int *p[3];

int a[3][4];

for(i=0;i<3;i++)

p[i]=a[i];

这里int *p[3] 表示一个一维数组内存放着三个指针变量,分别是p[0]、p[1]、p[2]

所以要分别赋值。

这样两者的区别就豁然开朗了,数组指针只是一个指针变量,似乎是C语言里专门用来指向二维数组的,它占有内存中一个指针的存储空间。指针数组是多个指针变量,以数组形式存在内存当中,占有多个指针的存储空间。

还需要说明的一点就是,同时用来指向二维数组时,其引用和用数组名引用都是一样的。

比如要表示数组中i行j列一个元素:

*(p[i]+j)、*(*(p+i)+j)、(*(p+i))[j]、p[i][j]

优先级:()>[]>*

sizeof分析

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/6/3
*/
int _tmain(int argc, _TCHAR *argv[])
{
    // array pointer
};
    ];
    p = a;

// test sizeof
    // for a
    cout << sizeof(a) << endl; // 24
) << endl; // 4  (24 in debug local watch)
) << endl; // 4  (24 in debug local watch)

cout << ]) << endl; // 12
]) << endl; // 12
)) << endl; // 12
)) << endl; // 12
]) << endl; // 4

// for p
    cout << sizeof(p) << endl; // 4
) << endl; // 4
) << endl; // 4

cout << ]) << endl; // 12
]) << endl; // 12
)) << endl; // 12
)) << endl; // 12
]) << endl; // 4

;
}

5. 函数指针

http://www.cnblogs.com/nezha/p/3204410.html

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/6/3
*/
typedef int (*func)(char a[], int nLength);

int total(char a[], int nLength)
{
    ;
    ; i < nLength; ++i)
        nTotal += a[i];
    return nTotal;
}

int main()
{
    char a[] = "abcde";
    int nLength = strlen(a);
    func fp;
    fp = total;
    printf("%d\n", fp(a, nLength));
    ;
}

【本文链接】

http://www.cnblogs.com/hellogiser/p/pointer-summary.html

最新文章

  1. BCS datetime 时间区间问题
  2. IOS开发--常用的基本GDB命令
  3. 【Linux】部署apache
  4. 如何为ios酷我音乐盒下载导出的音乐文件(使用Java程序设计)
  5. PHP编程----猴子选大王
  6. VopSdk一个高逼格微信公众号开发SDK:自动化生产(装逼模式开启)
  7. NodeJs的包漏洞扫描与漏洞测试攻击
  8. springboot+swagger2
  9. TComboBox组件重要属性和事件
  10. CSS3新增伪类汇总
  11. ORACLE中Scalar subquery Caching的hash table大小测试浅析
  12. python之路--初识html前端
  13. java中获取request与response对象的方法
  14. SPRINT四则运算(第二天)
  15. 使用fiddler模拟http请求
  16. Django CSRF提交遇见的问题
  17. vue-7-表单
  18. nginx动态配置及服务发现那些事
  19. 06-matplotlib-饼状图
  20. 19-spring学习-springMVC环境配置

热门文章

  1. maven_创建quickstart模板时异常
  2. Solr -- 实时搜索
  3. 【CodeForces 621C】Wet Shark and Flowers
  4. 苹果开发者账号如何多人协作进行开发和真机调试XCode
  5. NOIP2011 普及组 T3 洛谷P1309 瑞士轮
  6. 在使用easyui,datagrid时,JSON中的如果含有换行符,则不能显示数据
  7. 代码注册广播接收者&amp;利用广播调用服务的方法服务声命周期(混合开启)
  8. java中获取路径中的空格处理(%20)问题
  9. 改变placeholder颜色
  10. Jquery 扩展获取RUL参数