吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1653|回复: 19
收起左侧

[学习记录] CPP [学习笔记] 从难点!! C++指针开始(后面学习的时候发现指针一看就会一用就...

  [复制链接]
zds212 发表于 2021-5-9 12:14
1.指针概念
[C++] 纯文本查看 复制代码
#include<iostream>
using namespace std;
#include<string>
int main()
{
	//1.定义指针
	string name = "李白";//定义个一个数据
	string *temp;//创建指针
	temp = &name;//让temp的指针记录a的地址   &是取地址符号
	cout << "name的地址:" << &name << endl;//打印出name地址  测试结果
	cout << "temp的地址:" << temp << endl; //打印出temp地址  测试结果
	system("pause");
    return 0;
}



[C++] 纯文本查看 复制代码
#include<iostream>
using namespace std;
#include<string>
int main()
{
	//1.定义指针
	string name = "李白";//定义个一个数据
	string *temp;//创建指针
	temp = &name;//让temp的指针记录a的地址   &是取地址符号
	cout << "name的地址:" << &name << endl;//打印出name地址  测试结果
	cout << "temp的地址:" << temp << endl; //打印出temp地址  测试结果
	//2.使用指针
	//通过(解引用)的方式来找到指针指向的内存
	//指针前面加{【*】代表解引用},找到指针中所指向的内存数据。
	*temp = "比如我改了这个变量的内容";
	cout<<"那现在的变量name的内容为:"<<name<<endl;
	cout<<"解引用后的是内存中的数据   *temp"<<*temp<<endl;
	system("pause");
    return 0;
}





指针概念.png
指针概念1.png
指针概念2.png

免费评分

参与人数 2吾爱币 +3 热心值 +2 收起 理由
苏紫方璇 + 3 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
林中景 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

bester 发表于 2021-5-9 12:27
本帖最后由 bester 于 2021-5-9 12:28 编辑

我给你加一句话,指针的实质就是以指针类型去读一个地址的实际值,像文中string *temp  就是去读保存在temp这个地址中的实际内容,该地址的内容的实际类型是string,temp=取得name的地址

免费评分

参与人数 1吾爱币 +3 热心值 +1 收起 理由
ycf0616 + 3 + 1 用心讨论,共获提升!

查看全部评分

liangqz 发表于 2021-5-9 12:53
 楼主| zds212 发表于 2021-5-9 12:58
本帖最后由 zds212 于 2021-5-9 13:00 编辑

const常量指针
const修饰指针的三种情况
1.cont修饰指针  常量指针
2.cont修饰常量  指针常量
3.cont即修饰指针,又修饰常量


[C++] 纯文本查看 复制代码
#include<iostream>
using namespace std;
#include<string>
int main()
{
        int a = 10;
        int b = 10;
        //1.常量指针
        const int* temp = &a;
        temp = &b;//正确的,可以改地址
        //*temp=100;          temp已经const掉了,内存中的数据不能修改

        //2.指针常量
        int* const temp2 = &a;
        //temp2=&b;             temp2被const掉。所以temp不能修改地址

        //3.const修饰指针修饰常量
        const int* const temp3 = &a;
        //temp3=&b;            temp3的地址被定为常量不能修改
        //*temp3=100;          temp3的指针的所指向的内容被定为常量不能修改

        system("pause");
    return 0;
}
常量指针.png 指针常量.png
 楼主| zds212 发表于 2021-5-9 13:14
指针数组

[C++] 纯文本查看 复制代码
#include<iostream>
using namespace std;
#include<string>
int main()
{
        int arr[5] = { 5,1,4,3,2 };
        int* temp = arr;                 //arr是数组的首地址
        cout << *temp << endl;           //解引用数组中的首个元素    5

        int len = sizeof(arr) / sizeof(arr[0]);
        for (int i = 0; i < len; i++)
        {
                cout << *temp << endl;           //现在是数组中的第二个元素   1
                temp++;                          //=temp+1一个元素   往后一个元素
        }
        system("pause");
    return 0;
}

 楼主| zds212 发表于 2021-5-9 13:25
指针函数

[C++] 纯文本查看 复制代码
#include<iostream>
using namespace std;
#include<string>

void test(int *a1,int *b1)  // 相当于 *a1=&a  ,*b1=&b  
{
    //int temp = a1;不用*是错误的  需要解引用
    int temp = *a1;     //刚创建的temp是正常的变量  不用解引用
    *a1 = *b1;     
    *b1 = temp;
}

int main()
{
    int a = 1;
    int b = 2;
    test(&a, &b);   //传入a和b的地址到test的函数里
    cout << a << "\n" << b << "\n";
    system("pause");
        return 0;
}
 楼主| zds212 发表于 2021-5-9 13:28
bester 发表于 2021-5-9 12:27
我给你加一句话,指针的实质就是以指针类型去读一个地址的实际值,像文中string *temp  就是去读保存在temp ...

多谢大佬。 多多指教
 楼主| zds212 发表于 2021-5-9 13:53
指针 和    结构体  关键字struct   
[C++] 纯文本查看 复制代码
利用               ->           符号访问


[C++] 纯文本查看 复制代码
#include<iostream>
using namespace std;
#include<string>

struct house// 房子
{
        string parlour;
        string bedroom;
        string kitchen;
};

int main()
{

        struct house hou = {"客厅","卧室","厨房"};                 //创建结构体变量,并赋值
        struct house* temp = &hou;                           //通过指针指向结构体变量
        cout << temp->parlour <<temp->bedroom<< endl;        //通过指针访问结构体变量中的数据
        cout << hou.kitchen << endl;                         //直接访问结构体中的数据
        system("pause");
        return 0;
}
vethenc 发表于 2021-5-9 14:01
收藏吃灰
 楼主| zds212 发表于 2021-5-9 14:17
new运算符    堆区开辟   以及数组的释放方式 delete[]  数组名
[C++] 纯文本查看 复制代码
#include<iostream>
using namespace std;
#include<string>
#include<ctime>//时间头文件
//1.new的基本语法
int* func()
{
    //在堆区创建整型数据
    //new返回的是 该数据的指针
    int* p = new int(1);
    return p;

}
void test()
{
    int* p = func();
    cout << *p << endl;
    delete p;
    //cout << *p << endl; 已经释放了p的内存
}



//--------------------------



//数组堆区开辟
void test02()
{
    srand((unsigned int)time(NULL));//时间种子
    //在堆区创建数组
    int* arr = new int[5];//5代表5个元素
    for (int i = 0; i < 5; i++)
    {
        arr[i] = rand() % 41 + 60;//给数组赋值 60到100的随机数
    }
    for (int i = 0; i < 5; i++)
    {
        cout << arr[i] << endl;
    }

    delete[] arr;              //数组的释放   delete[] 数组名
}
int main()
{
    test();
    test02();
    system("pause");
    return 0;
}
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-15 21:35

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表