当前位置:网站首页>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 .边栏推荐
- RHCE's at, crontab's basic operations, the Chrony service, symmetric encryption and asymmetric encryption
- "Strangers once met" Summer Street Shen Shuyan_ Xia Mo Shen Shuyan's latest chapter
- Deep understanding of Base64 underlying principles
- Flask framework operation database_ Add, delete, modify and query statements
- Sunflower senior product director technology sharing: "how to apply national remote control" in AD domain environment
- Ma Zhixing entered the mass production of front loading, starting with the self-developed domain controller?
- 谁能详细说下mysqlRC下的半一致读和怎么样减少死锁概率?
- JS realizes the function of one click Copy
- 1. Header file - Comment - namespace - standard input / output stream
- 关于ALV格式控制部分的写法
猜你喜欢

Shopify卖家:EDM营销就要搭配SaleSmartly,轻松搞定转化率

Ribbon principle analysis namedcontextfactory

Casbin入门

Simple cases of inner connection and left connection

Zhihuijun, a genius of Huawei, made a modular mechanical keyboard, which caused an earthquake in the geek circle. Netizens: This is the real customization

How to understand "page storage management scheme"

CUB_200鸟类数据集关键点可视化

Beijing post network research 2015 problem2

Lvs+keepalived high availability deployment practical application

数据挖掘——关联分析基础介绍(上)
随机推荐
关于ALV格式控制部分的写法
Process tracking of ribbon principle
从2019 年开始,你一定停止使用了这个营销策略…
Vs code must know and know 20 shortcut keys!
Summary on the thought of double pointer
面试必备!TCP协议经典十五连问!
Casbin入门
The output comparison function of Tim is introduced in detail through PWM breathing lamp and PWM controlled DC motor
How fast does it take to implement a super simple language
[BGP] small scale experiment
谁能详细说下mysqlRC下的半一致读和怎么样减少死锁概率?
Let variable declaration feature of ES6 new feature and its case
[principle] several ways of horizontal penetration
Some notes on uniapp
First knowledge of C language (3)
Who can elaborate on the semi consistent read under mysqlrc and how to reduce the deadlock probability?
Ribbon principle analysis namedcontextfactory
Data mining -- Introduction to the basis of association analysis (Part 1)
MySQL Part 3
UCOS任务切换过程