IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> c++指针基础练习相关记录 -> 正文阅读

[C++知识库]c++指针基础练习相关记录

一. int* pt; pt = &higgens;

#include <iostream>
#include <cstdio>
#include<string>
#include<Cmath>
#include<CString>
using namespace std;

int main()
{
	int higgens = 5;
	int* pt;
	pt  = &higgens;

	cout << " &higgens  " << &higgens << endl;   //higgens的地址
	cout << "  pt " << pt << endl;   //打印的是指针,即higgens的地址
	cout << " * pt " << *pt << endl;  //打印的是值,不是指针
	return 0;
}

二.指针和数组的本质区别
1.p3 = p3 + 1;

int main()
{
	double* p3 = new double[3];
	p3[0] = 0.2;   //p3就相当于数组名,但是不是数组名,它实质上是指针
	p3[1] = 0.5;
	p3[2] = 0.8;
	cout << " p3[0] : " << p3[0] << endl;    //0.2
	p3 = p3 + 1;   //只有指针可以这么做,数组名是不能做运算的,加1的作用是将p3的第一个元素指向第二个元素,后面的依次向后移动
	cout << "  now p3[0] is : " << p3[0] << endl;  //0.5
	cout << " p3[1] : " << p3[1] << endl;       //0.8
	delete[] p3;
	return 0;
}

2.*(stacks + 1)、 stacks[1]

int main()
{
	double wages[3] = { 10000.0, 20000.0, 30000.0 };
	double stacks[3] = { 3,2,1 };

	//展示两种获取数组地址的方式
	double* pw = wages;
	double* ps = &stacks[0];

	//这里pw是数组的地址,也是数组第一个元素的首地址,*pw不再表示定义指针,而是取数组的第一个元素的值
	cout << " pw : " << pw << "  *pw : " << *pw << endl;   
	cout << " ps : " << ps << "  *ps : " << *ps << endl;   
	pw = pw + 1;
	ps = ps + 1;   

	cout << " pw : " << pw << "  *pw : " << *pw << endl;    
	cout << " ps : " << ps << "  *ps : " << *ps << endl;   

	//将指针指回第一个元素
	pw = pw - 1;
	ps = ps - 1;
	cout << " pw : " << pw << "  *pw : " << *pw << endl;
	cout << " ps : " << ps << "  *ps : " << *ps << endl;

	cout << " stacks[0] : " << stacks[0] << "  stacks[1] : " << stacks[1] << endl;
	cout << "  *stacks : " << *stacks << " *(stacks + 1) : " << *(stacks + 1) << endl;
	return 0;
}

在这里插入图片描述
3.指针指向字符串,操作和数组基本一致

int main()
{
	char animal[20] = "bear";
	const char* bird = "wren";
	char* ps;

	cout << animal << " and " << bird << endl;
	cout << *(bird +1) << endl;
	return 0;
}

在这里插入图片描述
4.创建动态结构体

struct inflatable
{
	char name[20];
	float volumn;
	double price;
};
int main()
{
	inflatable* ps = new inflatable; //分配动态结构体内存
	cout << "enter the name of the inflatable:";
	cin.get(ps->name, 20);
	cout << "enter the volumn of the inflatable:";
	cin >>(*ps).volumn;
	cout << "enter the price of the inflatable:";
	cin >> ps->price;

	cout << "name:" << (*ps).name << endl;
	cout << "volumn:" << (*ps).volumn << endl;
	cout << "price:" << (*ps).price << endl;
	delete ps;
	return 0;
}

5 函数返回值为数组地址

char* getname();
int main()
{
	char *name;
	name = getname();
	cout << name << endl;
	delete[] name;
	return 0;
}

char *getname()
{
	char temp[80];
	cout << "enter the last name:";
	cin >> temp;
	char* pn = new char[strlen(temp) + 1];
	strcpy_s(pn, strlen(temp) + 1,temp);  //strcpy实现字符串的复制作用
	return pn;         //返回的是这个字符串的存储的地址,并不是值
}

6.array vector 数组

int main()
{
	//创建模板类vector
	vector<double> a2(4); //创建含有四个元素大小的vector;
	a2[0] = 1.0 / 3.0;
	a2[1] = 1.0 / 5.0;
	a2[2] = 1.0 / 7.0;
	a2[3] = 1.0 / 9.0;

	//模板类array
	array<double, 4> a3 = { 3.14,2.72,1.62,1.41 };
	array<double, 4> a4;
	a4 = a3;

	cout << "a2[2]" << a2[2] << endl;
	cout << "a3[2]" << a3[2] << endl;
	cout << "a4[2]" << a4[2] << endl;

	return 0;
}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-14 13:50:50  更:2021-08-14 13:52:06 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 6:21:57-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码