当前位置:网站首页>Is the array name a pointer
Is the array name a pointer
2022-07-29 04:00:00 【Xiaowa 123】
Conclusion : The array name is a pointer constant , It can only point to the address of the first element of the array . When an array is used as a function parameter , Can transform ( Some are also called degradation ) For a pointer , You can operate some pointers , such as a++ To read the next element , But the pointer it is converted to also points to this array .
When the array name is not used as a function parameter , Although it is a pointer constant , But it cannot be used completely as a pointer . When an array is a function parameter , It is used as a pointer .
#include <stdio.h>
#include <string.h>
int main(void)
{
int a[3] = { 1, 2, 3 };
printf("%d\n", *a);// The result is 1.
At this point, the array name looks like a pointer , But it can't be used on this pointer ++ operation .
Questions may arise at this time , It seems that the array name can also be used as a pointer when it is not used as a function parameter ? So why does the array name degenerate into a pointer when it is used as a function parameter .
actually , Array names are used like this : The array name is a pointer , But a pointer constant , Its essence as a pointer constant has nothing to do with whether it is a function parameter , The pointer constant represented by the array name , Is bound to the corresponding array , Cannot point to other arrays , So when the array name is not used as a function parameter , Can not have a++ Such operation .
When the formal parameter of a function is an array , In the function body, this formal parameter can be a++ Such operation , Because function parameters are arrays , You can already know a++ It doesn't mean to let a This pointer constant points to other addresses , But through a++ Read the next element , So in the function body with array as function parameter ,a++ This kind of operation is possible , And at this point sizeof(a), Forever 4.
When an array is used as a function parameter , There is no need to specify the size of the array , Because no matter how many elements there are in the array , When an argument is passed in , The address of the first element is always passed in .
printf("%d\n", sizeof(a));//12, At this time, the array name means this array .
a++;// This is wrong , Array name a It's a pointer constant , It can't be changed , So the array name is not a pointer .
return 0;
}
void function(int a[])
{
a++;// This is OK again , Because when the array name is used as a parameter , It degenerates into a pointer , Self adding operation can be carried out .
}
in general , Whether or not it is an argument to a function , The array name is a pointer constant , Only when it is used as a function parameter , Because it has been defined that the parameters of the function are of array type , At this point, the array name will degenerate into a pointer , In the function body, it is treated as a pointer to this array .Take a look at this article , As a reference .https://www.cnblogs.com/youxin/p/3235862.html

Let's look at the following code :
#include <stdio.h>
#include <string.h>
int main(void)
{
int a[3] = { 1, 2, 3 };
int* p = a;
printf("%d\n", p);
int* p1 = &a;
printf("%d\n", p1);
return 0;
}
The two outputs are the same , You can find a That is, it can be regarded as the address of the first element of the array , It can also be regarded as the name of an array variable ,C It's a very flexible language .边栏推荐
- LDP --- 标签分发协议
- Why is continuous integration and deployment important in development?
- @Configuration (proxybeanmethods = false) what's the use of setting this to false
- VScode连接ssh遇到的问题
- Beijing post network research 2015 problem2
- Shopify seller: EDM marketing should be combined with salesmartly to easily get the conversion rate
- 第一个ALV程序2
- MySQL Part 4 (end)
- Typescript from entry to mastery (XXI) generic types in classes
- Process tracking of ribbon principle
猜你喜欢
随机推荐
Form verification of landline
[BGP] small scale experiment
Deep understanding of Base64 underlying principles
Routing knowledge
How fast does it take to implement a super simple programming language?
2. Variables and scope
The output comparison function of Tim is introduced in detail through PWM breathing lamp and PWM controlled DC motor
Remote desktop connection error
Beijing post network research 2015 problem2
通过PWM呼吸灯和PWM控制直流电机来详细介绍TIM的输出比较功能
Sunflower senior product director technology sharing: "how to apply national remote control" in AD domain environment
Summary on the thought of double pointer
[introduction to C language] zzulioj 1031-1035
[principle] several ways of horizontal penetration
EMD empirical mode decomposition
数据挖掘——关联分析基础介绍(上)
The list is not updated in real time when JS V-for data changes
Why is continuous integration and deployment important in development?
Lucifer 98 life record ing
Analysis of new retail o2o e-commerce model









