惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Project Zero
Project Zero
V
Vulnerabilities – Threatpost
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
C
Cisco Blogs
A
Arctic Wolf
月光博客
月光博客
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
量子位
小众软件
小众软件
Latest news
Latest news
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
N
Netflix TechBlog - Medium
K
Kaspersky official blog
人人都是产品经理
人人都是产品经理
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
Y
Y Combinator Blog
P
Proofpoint News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
S
Schneier on Security
D
Docker
Scott Helme
Scott Helme
MyScale Blog
MyScale Blog
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
GbyAI
GbyAI
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
H
Help Net Security
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tenable Blog
B
Blog
Know Your Adversary
Know Your Adversary
IT之家
IT之家

博客园 - 萧佰刚

Mtk Ft6306 touch 驱动 . 深圳市鑫侑触控技术有限公司 电容式触摸屏|电容式触摸屏厂|手机触摸屏厂|偏光片贴合 Qt Creator 常用快捷键 System V共享内存资料 "互斥锁用于上锁,条件变量则用于等待" Linux下UDP编程 Linux Socket编程学习 linux管道学习资料 linux进程间通信——消息队列 C实现单链表(转) c语言 位操作 C语言枚举类型使用简介 指向结构体指针的例子 字符数组 字符指针 sizeof strlen 的区别 Linux进程间共享临界区“信号量”编程 Linux下Socket编程 Linux下常用函数- 信号处理函数(转) linux信号signal常用信号 Linux 守护进程的编程方法(转)
结构体指针的指针使用(转)
萧佰刚 · 2011-07-21 · via 博客园 - 萧佰刚

结构体指针的指针使用

在C/C++中,自定义类,结构体等默认都是按值传递的,按值传递在函数中是无法改变参数的值的。

当函数要改变参数的值时,可以用指针传递参数,改变指针所指地址的值。

但是当我们要改变指针所指的地址时要怎么办呢?

这就要用到指针的指针了。例子:

#include <iostream>
using namespace std;

typedef 

struct node {
    
int data;
}node;
void change( node **p ) {
    
*= new node();
   (
*p)->data = 1;
}
int main()
{
    node 
*p;
    change( 
&p );
    cout 
<< p->data << endl;
    
return 0;
}

在函数中,要给 p 分配内存空间,所以要改变结构体指针 p 的值,如果用 change( node *p ),默认 p 是按引用传递,p 的地址不会改变。

用 change( node **p ) 传递指针的指针,在给参数时用取地址符号 &p 把储存指针的地址传递给函数。在函数中用复引用符号 *p 把储存指针的地址转化为指针。然后就可以改变指针所指的内存地址了。

还有一个要注意的问题是优先级问题。

-> 的优先级高于 * ,如果用没有加括号 *p->data = 1,相当于 *( p->data ) = 1。会编译出错。

所以必须加括号!