数组指针
int arr[4] = {1, 2, 3, 4};
int i;
int *p = arr;
int n = sizeof(arr)/sizeof(arr[0]);
for (i =0; i < n; i ++) {
cout << *(p+i) << endl;
}
cout << 1[arr] << endl;
count << *(1 + arr) << endl;
[] 是*() 的缩写, [] 左边的值,放在+ 号左边, []里面的值, 放在 右边
存储区

字符串指针

一个是栈区,一个是常量区
char *str = (char *)"hello word";
cout << str[4] << endl;
char *p = str;
cout << sizeof (p) << endl;
str[4] = 'h';
cout << str << endl;
指针数组
int a = 0;
int b = 1;
int c = 2;
int *arr[3] = {&a, &b, &c};
cout << arr[0] << endl;
cout << *arr[0] << endl;
cout << *arr[0]+ 1 << endl;
字符串数组
char *arr[4] = {"hahaha", "hehehe", "xixixi", "lalala"};
cout << arr[0] << endl;
cout << *arr << endl;
cout << arr[0]+ 1 << endl;

多级指针
int a = 0;
int *p = &a;
int **q = &p;
cout << *q << endl; //p的值
cout << **q << endl; // a的值
cout << q << endl; //p的地址
cout << p << endl;
}

数组指针
- 对数组首元素地址, &arr[0] arr + 1 跳过一个元素
- 对数组首地址, &arr arr +1 跳过一整个数组

int (*p)[3] = NULL;
int arr[3] = {5, 2, 3};
p = &arr;
cout << sizeof(*p) << endl;
cout << *(*p + 1) << endl;


https://www.bilibili.com/video/BV1gS4y1872b/?p=75&vd_source=c7a17cdbe5b7a027401c329724b632e9

初始化列表
构造函数是 对象成员的-》 类对象

单例写法
#include <iostream>
#include <string>
using namespace std;
void log(string str) {
cout << str << endl;
}
class Data {
private:
static Data *const p;
Data(){};
public:
int a;
static int b;
static Data * _instance();
void show();
};
Data * const Data::p = new Data;
Data * Data::_instance(){
return Data::p;
}
void Data::show(){
cout << "show singleTon"<< endl;
}
友元
#include <iostream>
#include <stdio.h>
#include "link.h"
#include "log.cpp"
#include <string.h>
using namespace std;
//friend
class Room{
friend void enterRoom(Room &r);
private:
void bedRoom();
string bedName;
public:
void setingRoom();
string setName;
Room(string bed, string setName) {
this->bedName = bed;
this->setName = setName;
};
};
void enterRoom(Room &room){
cout << "into" << room.bedName << endl;
}
int main(int argc, char *argv[])
{
Room room("bed","seting");
enterRoom(room);
}
- 不能被继承
- 单向的关系,A 是B的友元, B 不一定是A的
- 不能传递
这篇博客深入探讨了C++中的指针与数组的关系,包括数组指针的使用、字符串指针、指针数组、字符串数组以及多级指针的概念和操作。通过实例展示了如何访问数组元素、改变字符串内容以及进行多级指针解引用。还提到了友元在类设计中的作用以及单例模式的实现。

被折叠的 条评论
为什么被折叠?



