当前位置:网站首页>The return value of the function is the attention of the pointer, the local variables inside the static limit sub function, and how the pointer to the array represents the array elements
The return value of the function is the attention of the pointer, the local variables inside the static limit sub function, and how the pointer to the array represents the array elements
2022-07-29 04:02:00 【Xiaowa 123】
This content mainly studies pointer type functions, which need to pay attention to when returning pointers : Do not return a pointer to a local variable in a function .
I wanted to simply write a function that modifies the pointer type of the array element value through the pointer , As a result, many problems were exposed , It shows that the basic skills are not solid enough .
So let's see : When the return value type of a function is a pointer :
Look at the program first :
#include<stdio.h>
#include<string.h>
int* GetNum()
{
int array[10];
for (size_t i = 0; i < 10; i++)
{
array[i] = i;
}
return array;
【 Be careful 1】: Here the compiler prompts a warning , Returns the address of a local or temporary variable .
Here we return the address of the local array variable in this function , But in function return Then this function ends execution ,
The stack space where the array is located will be emptied , Although the address is still , But the above value is no longer guaranteed , therefore : Be careful : Do not return the address of the variable on the internal stack memory of the function !
}
int main(int agrc, char* argv[])
{
int* p = GetNum();
for (size_t i = 0; i < 10; i++)
{
printf("p[%d]=%d\n", i, *(p + i));
}
for (size_t i = 0; i < 10; i++)
{
printf("p[%d]=%d\n", i, *p[i]);
【 Be careful 2】 The operation of this step is to think of the pointer p The result of the variable is the first address of an array element ,
So I mistakenly think of the pointer p It can be used completely as an array name , So there was p[i] This way of writing ! Be careful ,p It's a pointer variable ,
It's not a pointer array , How can I write p[i] This kind of thing comes . People say that when the pointer points to an array ,
Yes, it is *(p+i) To address the next element , No *p[i], The second way of writing is completely wrong .
For array names ,int array[10]; use *(a+i) perhaps array[i] Fine ,
That's because the array name itself is a pointer !!!
remember , For pointer variables that are not array names and point to arrays , Only use *(p+i)!
}
return 0;
}If you want to achieve the above functions , There are two ways to be right :
1. In the sub function, the forward pointer operates , stay main Define an array in the calling function , hold main The array pointer in the function is passed to the sub function , In this way, array variables are opened up in main In the stack area of the function , The value on the address will not be released until the end of the whole program . as follows :
#include<stdio.h>
int* GetNum(int* arry)
{
for (size_t i = 0; i < 10; i++)
{
*(arry + i) = i;
}
return arry;
}
int main(int agrc, char* argv[])
{
int a[10];
int* p = GetNum(a);
for (size_t i = 0; i < 10; i++)
{
printf("p[%d]=%d\n", i, *(p + i));
}
return 0;
}2.static, Methods set as static variables . This method can return the address of the local variable of the function , But this local variable should be declared static Static variables , Stored in static storage area , Not released until the end of the program ! The following code :
#include<stdio.h>
#include<string.h>
int* GetNum()
{
static int array[10];
although array[] The array is defined inside the function , But because static, It is actually stored in static storage , Even if GetNum Call complete , The value in its memory space will not be released .
for (size_t i = 0; i < 10; i++)
{
array[i] = i;
}
return array;
【 Be careful 3】: Here the compiler will no longer report errors , Because although the address of the internal variable of the function is returned , But this variable is a static variable , Put it in the static storage area ,
Even if the function call ends , The memory space will not be released , Until the end of the program , The memory space will be released , So we can still get the correct value .
}
int main(int agrc, char* argv[])
{
int* p = GetNum();
for (size_t i = 0; i < 10; i++)
{
printf("p[%d]=%d\n", i, *(p + i));
}
return 0;
}In conclusion : If the return type of a function is a pointer , You cannot return the address of a non static variable of a local variable defined in a function . Either main The address of the local variable in the function is used as the input parameter to the function of pointer type ; Or you can define local variables inside the subfunction , And return the address of the local variable , But the local variable should be declared as static Formal .
// In fact, from the above two methods , The first one is recommended , Because there is no need to store some values in the static storage area , Anyway ,main Local variables in functions always exist on the stack ( It's just personal understanding )
Or the foundation is not solid , A lot of stupid little questions , Need more practice .
边栏推荐
- [redis series] string data structure
- Pointer constant and constant pointer
- CUB_ Visualization of key points in 200 bird dataset
- Spark dataframe replaces empty characters (or other values) in each column with null
- 【深度学习CPU(番外篇)——虚拟内存】
- When defining an array, the size must be constant
- 企业网的三层架构
- LDP -- label distribution protocol
- STM32F103ZET6程序移植为C8T6+C8T6下载程序flash timeout的解决方案
- Const char* and char*, string constants
猜你喜欢
随机推荐
Typescript from getting started to mastering (XVI) configuration file - first knowledge of compileroptions configuration item
With more than 5 years of work experience and a salary of 15K, would you accept it if you were me?
Form verification of landline
Malloc C language
有一种密码学专用语言叫做ASN.1
谁能详细说下mysqlRC下的半一致读和怎么样减少死锁概率?
Ma Zhixing entered the mass production of front loading, starting with the self-developed domain controller?
这个报错是什么鬼啊,不影响执行结果,但是在执行sql时一直报错。。。连接maxComputer是使用
How to understand "page storage management scheme"
[principle] several ways of horizontal penetration
EMD 经验模态分解
Nacos registry
当我从数据库获取到了winfrom特定的控件ID之后我需要通过这个ID找到对应的控件,并对控件的TEXT文本进行赋值这该怎么做
Who can elaborate on the semi consistent read under mysqlrc and how to reduce the deadlock probability?
Const read only variable constant
How to write SQL statements about field conversion
Communication between parent-child components and parent-child components provide and inject
MySQL Part 4 (end)
When defining an array, the size must be constant
Pointer constant and constant pointer


![[原理] 横向渗透的几种方式](/img/fc/2ef7dd6ebc5c0bd8f7d302d8b596d6.png)






