最近写程序的时候,发现自己很多东西都开始忘了。今天终于有机会好好总结一下指针,当做个笔记同时也希望对大家有用。如果有不正确的地方,希望大家能帮我指正一下。然后我的实验环境是32位RHEL+eclipse。
一、指针基本属性
指针的属性主要包括指针的类型、指针所指向的类型、指针的值。下面以一个简单的例子为例
int *p;
指针的类型:int *
指针所指向的类型:int
指针的值(指针所指向内存区的地址):野指针
总结一下,指针的类型:去掉变量名字之后的那一部分
指针所指向的类型:去掉*变量名之后的那一部分
指针的值:指针所指向内存区的地址
最后这里还有一个小点,指针是否站内存空间。答案是肯定的,我们做一个简单的实验测试一下:
/*
============================================================================
Name : test.c
Author : grublinux
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
typedef
struct
node
{
int
data
;
struct
node
*
next
;
};
int
main
(
void
)
{
printf
(
"Ths size of char * is %d
\n
"
,
sizeof
(
char
*
));
printf
(
"Ths size of int * is %d
\n
"
,
sizeof
(
int
*
));
printf
(
"Ths size of node * is %d
\n
"
,
sizeof
(
struct
node
*
));
return
0
;
}
这里为什么都是4字节呢?因为我们指针的存储内容是内存地址,而我的机子是32位。32位就是4个字节!
注:区分指针的类型和 指针所指向的类型是精通C的关键
二、&和*运算符
&运算符叫做取地址运算符,*运算符叫做间接运算符(取内容运算符)。他们和指针结合在一起的时候,经常让我们无比头疼。像段错误,很多时候也是他们使用不当导致的。这里引用一个比较好的总结:
&p的运算结果是一个指针,指针的类型是p的类型加个*。指针所指向的类型是p的类型,所指向的地址就是p1的地址。
*p的运算结果是p所指向的东西,他的类型是p指向的类型,它的地址是p所指向的地址。
理论总是难以理解的,我们来做做实验:
这里为什么都是4字节呢?因为我们指针的存储内容是内存地址,而我的机子是32位。32位就是4个字节!
这里为什么都是4字节呢?因为我们指针的存储内容是内存地址,而我的机子是32位。32位就是4个字节!
注:区分指针的类型和 指针所指向的类型是精通C的关键
/*============================================================================Name : test.cAuthor : grublinuxVersion :Copyright : Your copyright noticeDescription : Hello World in C, Ansi-style============================================================================*/#include <stdio.h>#include <stdlib.h>typedef struct node {int data ;struct node * next ;};int main ( void ) {int n = 4 ;int * p ;int ** ptr ;printf ( "Test one : \n " );printf ( "The answer of &n is %x \n " , & n );printf ( "The answer of &p is %x \n " , & p );printf ( "The answer of &ptr is %x \n " , & ptr );p =& n ;ptr =& p ;printf ( "Test two : \n " );printf ( "The value of p is %d \n " , * p );printf ( "The address of p is %x \n " , & p );printf ( "The address of p point to is %x \n " , p );printf ( "Test three : \n " );printf ( "The value of ptr is %x \n " , * ptr );printf ( "The address of ptr is %x \n " , & ptr );printf ( "The address of ptr point to is %x \n " , ptr );return 0 ;}
&n的运算结果事一个指针,指针的类型是int *,指针所指向的类型是int。(和等号左边的p信息匹配上了)
&p的运算结果事一个指针,指针的类型是int **,指针所指向的类型是int *。(和等号左边的ptr信息匹配上了)
printf("The value of p is %d\n",*p);
由于之前的p=&n,所以p指向的是n的地址。但是&p(p本身自己的地址)没有变化。所以*p的值就是p指向的地址的内容,也就是4。*p的类型是int,所以这里用%d进行输出。
printf("The value of ptr is %x\n",*ptr);
同理,这里ptr指向的是p的地址。但是&ptr(ptr本身自己的地址)没有变化。所以*ptr的值就是ptr指向的地址(p的地址)的内容,也就是n的地址。*ptr的类型是int *,所以这里用%x进行输出。
更加简单直白地说,*就是降*操作符,&是升*操作符。
三、指针和数组
在有了上面的基础之后,我们继续来看看指针和数组。先看看下面的测试代码:
/*============================================================================Name : test.cAuthor : grublinuxVersion :Copyright : Your copyright noticeDescription : Hello World in C, Ansi-style============================================================================*/#include <stdio.h>#include <stdlib.h>typedef struct node {int data ;struct node * next ;};int main ( void ) {int n = 4 ;int * p ;int ** ptr ;printf ( "Test one : \n " );printf ( "The answer of &n is %x \n " , & n );printf ( "The answer of &p is %x \n " , & p );printf ( "The answer of &ptr is %x \n " , & ptr );p =& n ;ptr =& p ;printf ( "Test two : \n " );printf ( "The value of p is %d \n " , * p );printf ( "The address of p is %x \n " , & p );printf ( "The address of p point to is %x \n " , p );printf ( "Test three : \n " );printf ( "The value of ptr is %x \n " , * ptr );printf ( "The address of ptr is %x \n " , & ptr );printf ( "The address of ptr point to is %x \n " , ptr );return 0 ;}
要了解步长的变化规律,首先我们要清晰地得到数组的元素类型。其实也非常简单,就是去掉变量名剩下的
就是数组元素类型:
char *array[10];//元素类型是char *
int test[10];//元素类型是int
那什么是上一级元素类型,其实就是元素类型去掉一个*。如果没有*了,就表示已经是顶级。
char *array[10];//元素类型是char *,上级元素是char
int test[10];//元素类型是int,顶级
最后就是步长的变化规则:
1)当变量名每和*结合一次。步长变成上级元素的长度。
2)当顶级元素再和*结合,步长变为1,1的类型是常规的int。
3) 初始步长为数组元素类型长度
所以,*(array+1)的步长是sizeof(char *) //这里因为没有和*结合过,所这里步长是sizeof(char *)
*array+1的步长是sizeof(char) //这里因为结合了一次,所以这里的步长是上级元素类型char的长度
*(test+1)的步长是sizeof(int) //这里因为没有和*结合过,所以步长是sizeof(int)
*test+1的步长是整型1,也就是说如果*test的值时4那*test+1的值就是5 //这里因为和*结合了一次之后,发现int已经是顶级元素了。不能再升级,所以这里编译器把它默认为整型。
四、指针和结构体
还是老规矩,先晾代码再分析:
/*============================================================================Name : test.cAuthor : grublinuxVersion :Copyright : Your copyright noticeDescription : Hello World in C, Ansi-style============================================================================*/#include <stdio.h>#include <stdlib.h>typedef struct node {int data ;struct node * next ;};int main ( void ) {struct node * p = NULL ;struct node n = { 1 , NULL };printf ( "The content of n is %d and %x. \n " , n . data , n . next );p = ( struct node * ) malloc ( sizeof ( struct node ));p -> data = 1 ; p -> next =& n ;printf ( "The address of n is %x. \n " , & n );printf ( "The content of p is %d and %x. \n " , p -> data , p -> next );printf ( "The content of n is %d and %x. \n " ,( * ( p -> next )). data ,( * ( p -> next )). next );return 0 ;}![]()
这里先清晰一些概念,指针通过—>访问结构体内部的变。而结构体本身是通过 . 来访问自己的变量的。
所以p指向新创建结构体之后,通过printf("The content of p is %d and %x.\n",p->data,p->next);来访问自己结构体内部的变量。
然后这里比较复杂的可能是:
printf("The content of n is %d and %x.\n",(*(p->next)).data,(*(p->next)).next);
这里p->next指向的是n的存储地址,然后我们用*取出了n的内容。然后通过.来访问n自身的变量。有没有感觉自己对指针熟悉了很多呢?O(∩_∩)O
五、指针的强制类型转换
/*============================================================================Name : test.cAuthor : grublinuxVersion :Copyright : Your copyright noticeDescription : Hello World in C, Ansi-style============================================================================*/#include <stdio.h>#include <stdlib.h>typedef struct node {int data ;struct node * next ;};int main ( void ) {printf ( "The size of node is %d. \n " , sizeof ( struct node ));struct node n = { 1 , NULL };int * p = ( int * ) & n ;printf ( "The value of n is %d and %x. \n " , * p , * ( p + 1 ));return 0 ;}
![]()
我们要分析出为什么能这样做,要先了解下面几个点:
1)结构体的构成
以这里结构体为例,int 类型是4个字节。struct node*类型是指针类型所以是4字节。因此有了第一个输出8.typedef struct node{int data;struct node *next;};2)强制类型转换之后的步长
强制类型转换之后,步长也发生了变化。因为起始元素类型(一模块中的指针指向的变量类型)变成了int,所以这里的步长是sizeof(int)也就是4字节。
printf("The value of n is %d and %x.\n",*p,*(p+1));
这里大家注意struct node的第一个变量int的步长也是4字节。所以(p+1)后从开始的起始地址刚好偏移了4字节。然后得到了变量*next的起始地址,然后通过*取出其内容。
到这来就结束啦!祝愿大家早日成为C语言的高手,如果上面有不正确的地方,或者看完还有什么疑惑。都欢迎大家指出来,一起讨论讨论!
转载请注明:http://blog.youkuaiyun.com/grublinux/article/details/28648377