Skip to content

Instantly share code, notes, and snippets.

@ustbgaofan
Last active December 18, 2015 01:29
Show Gist options
  • Save ustbgaofan/5704077 to your computer and use it in GitHub Desktop.
Save ustbgaofan/5704077 to your computer and use it in GitHub Desktop.
常量指针和变量指针
cosnt int* p; //p可变,p指向的内容不可变
int const* p;//p可变,p指向的内容不可变
int* const p;//p不可变,p指向的内容可变
const int* const p;//p和p指向的内容都不可变
口诀:左数右指
当const出现在*的左边时,指针指向的数据为常量
当const出现在*的右边时,指针本身为常量
#include <stdio.h>
int main()
{
int i=0;
const int* p=NULL;
p=&i;
*p=10;//编译时报错,p指向的数据为read-only
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment