当前位置:网站首页>Array as function parameter -- pointer constant / constant pointer
Array as function parameter -- pointer constant / constant pointer
2022-07-29 04:00:00 【Xiaowa 123】
It was always thought that array names were pointers , It's not . Only when the array name is used as a function parameter can it degenerate into a pointer of the same type with the same name , Pointer as a function parameter is equivalent to array name .( Be careful , Equivalence just means the same effect , Not equivalent , You can't think of an array name as a pointer , The pointer is the array name .)
The following procedure : There is a length of 4 Of float The array holds four numbers , This array is used as an argument to a function that calculates the average .
#include <stdio.h>
#include <string.h>
float Avarage(float array[4])
{
int i;
float aver;
float sum = array[0];
for ( i = 1; i <= 3; i++)
{
sum = sum + array[i];
}
aver = sum /4;
return aver;
}
int main(void)
{
float score[4] = { 10.5, 10.5, 10.5, 10.5};
printf("%.2f\n",Avarage(score));// The parameter here is if
// It's written in score[4] Will make mistakes , The reason for the mistake is Average The parameter of this function should be a float Pointer to type
// If the argument is score[4],float The actual participation of type float* Type has incompatible formal parameters .
// It's written in score[] It's not right either , Only score Yes. .
return 0;
}In defining Average() Function , If the function parameter is an array , It can be defined in this way ,Average() The formal parameters of this function are float An array of types , In fact, this formal parameter array is reduced to pointers in the next function body , therefore Average(float arrar[10] or float array[]) It's actually Average(float* array) !( When a function is defined , In fact, there is no need to specify the size of the shape parameter group , Because the allocation of specific memory space is not involved at this time .)
The following definitions are the same , In the following function body ,array This array name is actually a pointer variable with the same name float* array, and float* array The value of this pointer variable is float array[0] The address of .
#include <stdio.h>
#include <string.h>
float Avarage(float* array)// Notice here .
{
int i;
float aver;
float sum = array[0];
for ( i = 1; i <= 3; i++)
{
sum = sum + array[i];
}
aver = sum /4;
return aver;
}
int main(void)
{
float score[10] = { 10.5, 10.5, 10.5, 10.5};
printf("%.2f\n",Avarage(score));
/* You need to enter arguments when calling this function , Actually, it should be float* Variable of type , For example, a float The number of arrays ! Group ! name !, This argument array name is actually equivalent to a pointer */
return 0;
}http://blog.sina.com.cn/s/blog_7f69fbf90102whhe.html
Array as a function parameter , Do you want to allocate memory space for the array name when calling the function
There is no need to . The exact understanding of an array name is a pointer , And it's a Constant pointer , Fixed to the first element of the array it represents . When both arguments and formal parameters are arrays , When a call occurs, pass the constant address in the actual parameter array name to the shape parameter group name , That is to say, the shape parameter group name saves the address stored in the actual parameter array name . So inside the called function , The formal parameter array points to the same array as the argument array name .
Let's put it this way. : Although the formal parameter is an array , But in fact, the only parameter passed is the pointer constant of argument array name ( Pass this pointer to the shape parameter group name ). Of course, the formal parameter array name takes up memory space , But there is no need for ( Neither ) You allocate space to the called function ( This is done automatically by the compiler ). If you want to compile and call correctly , Just make sure that your argument array and formal parameter array are of the same type . That is to say , When you define the function header , There is no need to specify the size of the shape parameter group ( There is no appointment , Because the argument only passes a pointer to the name of the array )
边栏推荐
- 3.解决Pycharm报错Unresolved reference ‘selenium‘ Unresolved reference ‘webdriver‘
- Summary on the thought of double pointer
- Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
- lodash库常用方法
- 《陌路曾相逢》夏陌沈疏晏_夏陌沈疏晏最新章节
- Analysis of new retail o2o e-commerce model
- 内连接和左连接简单案例
- How to understand clock cycle and formula CPU execution time = number of CPU clock cycles / dominant frequency
- HCIP BGP
- 数据挖掘——关联分析例题代码实现(下)
猜你喜欢

消费行业数字化升级成 “刚需”,weiit 新零售 SaaS 为企业赋能!

Data mining -- Introduction to the basis of association analysis (Part 1)

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

Raft protocol - process demonstration

Configmap配置与Secret加密

1985-2020(8个版次)全球地表覆盖下载与介绍

Sunflower senior product director technology sharing: "how to apply national remote control" in AD domain environment

Shopify seller: EDM marketing should be combined with salesmartly to easily get the conversion rate

Batch production and upload sales NFT opensea eth polygon

小马智行进军前装量产,从自研域控制器入手?
随机推荐
基于STM32和阿里云的环境检测系统设计
力扣面试题17.04 消失的数字||260.只出现一次的数字(内含位运算知识点)
Typescript from entry to mastery (XVIII) joint type and type protection
企业网的三层架构
当我从数据库获取到了winfrom特定的控件ID之后我需要通过这个ID找到对应的控件,并对控件的TEXT文本进行赋值这该怎么做
Why do programmers so "dislike" the trunk development mode?
Typescript from getting started to mastering (XXIII) namespace namespace (Part 2)
tron OUT_ OF_ ENERGY
How to understand clock cycle and formula CPU execution time = number of CPU clock cycles / dominant frequency
Code ~ hide or disable the status bar and virtual keys
Asp.net MVC中文件夹中的控制器如何跳转到根目录的控制器中?
面试必备!TCP协议经典十五连问!
The function parameters of the new features of ES6 are assigned initial values and rest parameters
Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
Data mining -- Introduction to the basis of association analysis (Part 1)
Object array merges elements according to a field
Press the missing number of interview question 17.04 | | 260. the number that appears only once (including bit operation knowledge points)
Several cases of word wrapping in div
[原理] 横向渗透的几种方式
MySQL第三篇