当前位置:网站首页>C语言的指针
C语言的指针
2022-06-21 14:45:00 【用户6978604】
指针作为参数传递
C语言中,当指针作为函数参数进行传递时,实际上形参和实参指向的是同一个地方,例如:
/*
* @Author: YaleXin
* @Date: 2020-06-19 18:02:53
* @LastEditTime: 2020-06-29 08:59:40
* @LastEditors: YaleXin
* @Description:
* @FilePath: \my_c_workspace\some_test\pointTest-copy.c
* @祈祷不出现BUG
*/
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
void test(int *p1) { printf("=== test p1 = %p ====\n", p1); }
int main() {
int *p;
printf("=== main p = %p ====\n", p);
test(p);
return 0;
}输出的是:
=== main p = 0000000000000010 ====
=== test p1 = 0000000000000010 ====可见二者的地址是相同的。
所以形参对其指向的内存区域进行修改,也会影响到实参指向的内存区域,因为二者指向的区域是同一个位置,例如:
/*
* @Author: YaleXin
* @Date: 2020-06-19 18:02:53
* @LastEditTime: 2020-06-29 09:08:30
* @LastEditors: YaleXin
* @Description:
* @FilePath: \my_c_workspace\some_test\pointTest-copy.c
* @祈祷不出现BUG
*/
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
void test(int *p1) { *p1 = 2; }
int main() {
int a = 1;
int *p = &a;
printf("before a = %d\n", a);
test(p);
printf("after a = %d\n", a);
return 0;
}输出的是:
before a = 1
after a = 2但是,我们再来看一个例子:
/*
* @Author: YaleXin
* @Date: 2020-06-19 18:02:53
* @LastEditTime: 2020-06-29 09:16:55
* @LastEditors: YaleXin
* @Description:
* @FilePath: \my_c_workspace\some_test\pointTest-copy.c
* @祈祷不出现BUG
*/
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
void test(char *p1) { p1 = "modified"; }
int main() {
char *p = NULL;
test(p);
printf("after p = %s\n", p);
return 0;
}很容易让人以为输出的是
after p = modified但是实际上输出的是
after p = (null)下图给出这个过程中,指针的变化:
实际上是因为形参指向的区域已经与实参指向的不一致。
但是假如有在被调函数改变实参的地址的需求怎么办?
两种方法:
被调函数返回指针:
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
char *test(char *p1) { p1 = "modified"; }
int main() {
char *p = NULL;
p = test(p);
printf("after p = %s\n", p);
return 0;
}但是使用该方法的时候,不推荐返回局部指针,返回局部指针很容易出现错误,详见这篇文章
下面的例子就是存在问题:
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
char *test(char *p1) {
p1 = "modified";
char str[20] = "";
sprintf(str, "yalexin %s", p1);
printf("str = %s\n",str);
return str;
}
int main(void) {
char *p = NULL;
p = test(p);
printf("after p = %s\n", p);
return 0;
}输出的是:
str = yalexin modified
after p = (null)使用二级指针:
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
char test(char **p1) { *p1 = "modified"; }
int main(void) {
char *p = NULL;
printf("调用函数之前p的地址是 %p\n", p);
test(&p);
printf("调用函数之后p的地址是 %p\n", p);
printf("after p = %s\n", p);
return 0;
}输出:
调用函数之前p的地址是 0000000000000000
调用函数之后p的地址是 0000000000404000
after p = modified注意事项
- 当指针指向的是一个变量,例如
*int* *p = &a;,是不能使用free(p)进行释放资源,free()只是针对使用了malloc(),realloc()函数进行申请的内存进行释放。 - 使用
free()后,相应的指针就会变为“野指针”,如果对野指针进行引用,会造成不可预期的后果,所以一般使用free(p)后,一般紧接着p = NULL
边栏推荐
- 网上开户安全吗?新手可以开账户吗
- T32 add toolbar button
- Decipher Bishengyuan: is it tea that consumers buy, or is it IQ tax?
- UBI error: ubi_ read_ volume_ table: the layout volume was not found
- Num in tensorflow basiclstmcell_ What are units- What is num_ units in tensorflow BasicLSTMCell?
- CSDN is the only one: detailed tutorial teaching on how to connect multiple mobile phones by appium+pytest+allure+jenkins
- Numpy: basic package for high performance scientific computing & data analysis
- Write a code hot deployment
- Promotion guide for large enterprises: material preparation, PPT writing and on-site defense
- NPM package management configuration file [package.json and node\u modules configuration details and how to develop their own packages and publish them on NPM]
猜你喜欢

Reverse generate the corresponding DTD constraint according to the XML file

kernel GDB

Configuring MySQL master-slave and implementing semi synchronous replication in docker environment

Vscade, open a folder or workspace... (file - > open folder) solution
![[how to install MySQL 8.0 to a non system disk] [how to create a new connection with Navicat and save it to a non system disk] and [uninstall MySQL 8.0]](/img/e4/895cc829e34692a069923e226deb11.jpg)
[how to install MySQL 8.0 to a non system disk] [how to create a new connection with Navicat and save it to a non system disk] and [uninstall MySQL 8.0]

Use Matplotlib to draw the first figure

Simplified crud using code generation

2022 Fujian latest fire protection facility operator simulation test question bank and answers

So many statistical charts? This visualizer is great~~

USB message capture tcpdump
随机推荐
2021-2022 recall of the final exam questions of non relational database of Shandong University
Tcp/ip FAQs
Read distributed consistency protocols 2pc and 3pc
JS interview question: regular expression, to be updated
Niuke - real exercise-01
C language function fgets
JS written test question: array
Qt-3-basic assembly 2
Office operation sorting notes
T32 add toolbar button
Record the processing process of slow response of primary system
Vscade, open a folder or workspace... (file - > open folder) solution
What fun things can a desk service do
Summary of common libraries in machine learning
Is it safe to open a securities account by downloading the app of qiniu school? Is there a risk?
CSDN's test teacher teaches JMeter to generate stress test reports
Machine learning model training template
Qmetaobject:: connectslotsbyname: no matching signal for problem solving
Design interface automation test cases by hand: establish database instances and test case tables
Configuring MySQL master-slave and implementing semi synchronous replication in docker environment