一个“无聊”的关于指针的演示程序
2012-03-18 16:39
435 查看
#include <stdio.h> int main(int argc, char *argv[]) { // create two arrays we care about int ages[] = {23, 43, 12, 89, 2}; char *names[] = {"Alan", "Frank","Mary", "John", "Lisa"}; // safely get the size of ages int count = sizeof(ages) / sizeof(int); int i = 0; // first way using indexing for(i = 0; i < count; i++) { printf("%s has %d years alive.\n", names[i], ages[i]); } printf("---\n"); // setup the pointers to the start of the arrays int *cur_age = ages; char **cur_name = names; // second way using pointers for(i = 0; i < count; i++) { printf("%s is %d years old.\n", *(cur_name+i), *(cur_age+i)); } printf("---\n"); // third way, pointers are just arrays for(i = 0; i < count; i++) { printf("%s is %d years old again.\n", cur_name[i], cur_age[i]); } printf("---\n"); // fourth way with pointers in a stupid complex way for(cur_name = names, cur_age = ages;(cur_age - ages) < count; cur_name++, cur_age++){ printf("%s lived %d years so far.\n", *cur_name, *cur_age); } return 0; }
相关文章推荐
- 关于指针的一个小程序
- 关于指针的一个小程序
- 基于AjaxHelper0.41的相册范例程序演示及一个关于博客园的功能建议
- 基于AjaxHelper0.41的相册范例程序演示及一个关于博客园的功能建议
- 关于指针和形参结合的一个程序的分析
- 关于指针的一个小程序
- 关于指针的一个小程序
- 关于未在本地计算机上注册"Microsoft.Ace.Oledb.12.0"提供程序的一个细节
- 关于获取素数 一个小程序
- C角度(一)——任何C程序,可理解为指针指向某一个字符,然后字符匹配
- 一个关于二叉树的小程序
- 一个程序理解指针与结构的关系
- Java多区域部署时能自动升级的一个小演示程序
- 关于Spring中AOP的一个小例子程序
- 一个有意思的关于函数指针用在vector里的模型
- 由一个朋友遇到的问题想到的关于程序的架构
- 关于windows窗体FORM程序打包成一个文件
- 设ha和hb分别是两个带头节点的费递减有序单链表的表头指针,设计以算法,将将两个有序链表合成一个非递减的有序单链表,该程序以以前发表的博客中的链表中的区别在与该立案表带有头结点
- 一个关于c++数值转换的小程序的思考
- 关于 未能加载文件或程序集“ImageMagickNet”或它的某一个依赖项。试图加载格式不正确的程序 的解决办法