博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据结构(1):C语言总结
阅读量:7063 次
发布时间:2019-06-28

本文共 2847 字,大约阅读时间需要 9 分钟。

http://msdn.microsoft.com/en-us/library/cc262957%28v=office.12%29.aspx

指针:

#pragma once#include 
int main(void){
int stopFlag = 0; int a; int *aPtr; a = 7; aPtr = &a; printf("The address of a is %p\nThe value of aPtr is %p", &a, aPtr); printf("\nThe value of a is %d\nThe value of *aPtr is %d", a, *aPtr); printf("\n\nShowing that * and & are complements of each other\n&*aPtr = %p\n*&aPtr = %p\n", &*aPtr, *&aPtr); scanf("%d", &stopFlag);}

运行结果:

按值传递:

#pragma once#include 
int cubeByValue(int n);int main(void){ int stopFlag = 0; int number = 5; printf("The original value of number is %d", number); number = cubeByValue(number); printf("\nThe new value of number is %d", number); scanf("%d", &stopFlag);}int cubeByValue(int n){ return n * n * n;}

运行结果:

按引用传递:

#pragma once#include 
void cubeByReference(int *nPtr);int main(void){ int stopFlag = 0; int number = 5; printf("The original value of number is %d", number); cubeByReference(&number); printf("\nThe new value of number is %d", number); scanf("%d", &stopFlag);}void cubeByReference(int *nPtr){ *nPtr = *nPtr * *nPtr * *nPtr;}

运行结果:

最小权限原则(const限定符):

#1 指向非常量数据的非常量指针:

/*    using a non-constant pointer to non-constant data*/#pragma once#include 
#include
void convertToUppercase(char *sPtr);int main(void){ int stopFlag = 0; char string[] = "characters and $32.98"; printf("The string before conversion is: %s", string); convertToUppercase(string); printf("\nThe string after conversion is: %s", string); scanf("%d", &stopFlag);}void convertToUppercase(char *sPtr){ while(*sPtr != '\0'){ if (islower(*sPtr)){ *sPtr = toupper(*sPtr); } ++sPtr; }}

运行结果:

 

#2 指向常量数据的非常量指针:

/*using a non-constant pointer to constant data*/#pragma once#include 
#include
void printCharacters(const char *sPtr);int main(void){ int stopFlag = 0; char text[] = "print characters of a string"; printf("The string is:\n"); printCharacters(text); printf("\n"); scanf("%d", &stopFlag);}/*sPtr is a "read-only" pointer, cannot modify the character to which it points*/void printCharacters(const char *sPtr){ for (; *sPtr != '\0'; sPtr++){ printf("%c", *sPtr); }}

运行结果:

#3 指向常量数据的非常量指针(企图修改数据):

#4 指向非常量数据的常量指针

/*using a constant pointer to non-constant data*/#pragma once#include 
int main(void){ int stopFlag = 0; int x = 0; int y = 0; int* const ptr = &x; printf("The original value of x is: %d", *ptr); x = 5; printf("\nThe new value of x is: %d", *ptr); scanf("%d", &stopFlag);}

运行结果:

#5 指向非常量数据的常量指针(企图修改指针):

#6 指向非常量数据的非常量指针(企图修改数据和指针):

转载于:https://www.cnblogs.com/thlzhf/archive/2013/01/03/2842772.html

你可能感兴趣的文章
简易定制 Debian 软件仓库
查看>>
如何安装 gearmand 及对应的 php 扩展
查看>>
cocos2d-x引擎库binary版本制作(Windows环境)
查看>>
MongoDB副本集搭建
查看>>
Matlab中堆叠矩阵repmat的使用!
查看>>
设置 java -jar 的进程显示名称
查看>>
linux运维实战练习-2015年8月30日课程作业
查看>>
HDU 2019
查看>>
RabbitMQ
查看>>
项目规划管理 - 1
查看>>
×××案例之一路由之间的×××
查看>>
wamp配置多域名虚拟目录
查看>>
Android之ubuntu源码开发环境搭建笔记
查看>>
3、开启debug调试模式
查看>>
阅读摘要
查看>>
CURL POST JSON 数据,在linux平台下命令
查看>>
Ovirt 笔记
查看>>
webpack 4.x 搭建项目脚手架
查看>>
搭建IM服务 so easy
查看>>
高并发量网站解决方案
查看>>