指向常量的指针用法和注意事项
来源:互联网 发布:c调用windows api 编辑:程序博客网 时间:2024/05/21 14:01
指向常量的指针(point to const):不能通过指针对变量进行修改,但该指针可以指向其他地方。const int* p=&a; 最常用。
下面通过一个例子进行说明:
#include<stdio.h>
//void wrong(const char* str);
void correct(const char* str);
int main()
{
//wrong("this is a test");
correct("this is a test");
return 0;
}
/*void wrong(const char* str)
{
while(*str)
{
if(*str==' ');//遇到空格修改成—
*str='-';//这里修改了常量指针所指向的数值。这是不允许,所以报错。
printf("%c\n",*str);
str++;
}
}*/
void correct(const char* str)
{
while(*str)
{
if(*str==' ')
printf("%c",'-');//这是正确的,这个是遇到空格,直接用—覆盖。
else
printf("%c",*str);
str++;
}
}
0 0
- 指向常量的指针用法和注意事项
- 指向常量的指针和指针常量
- 常量指针和指向常量的指针
- 指向常量的指针和指针常量
- 指向常量的指针 和 指针常量
- 指向常量的指针和常量指针
- 指向常量的指针和指针常量
- 常量指针和指向常量的指针
- 常量指针和指向常量的指针
- 常量指针和指向常量的指针
- 常量指针和指向常量的指针
- 常量指针和指向常量的指针
- 指向常量的指针和常量指针
- 指针常量和指向常量的指针
- 指针常量和指向常量的指针
- 指向常量的指针和常量指针
- 指针常量和指向常量的指针
- 常量指针和指向常量的指针
- Linux新手入门:通过chmod改变文件权限
- AE+C#通过名称来删除要素类
- Intellij idea中properties文件无法自动编译到输出目录问题
- 从最大似然到EM算法浅解
- 各种排序方法总结
- 指向常量的指针用法和注意事项
- easyui中回车键实现关闭弹窗口和查询功能
- 本地音乐播放器
- Struts-sort标签
- iOS开发笔记1-URL重定向 URL redirect
- 实现word等在线预览(百度文库)
- SQL Server 设置远程访问的一系列问题
- android,NativeActivity使用
- 【数位DP】【poj 3252】Round Numbers