当前课程知识点:C Programming >  Chapter 8 Pointer >  8.4 Using Array Name as Function Parameter >  8.4 Using Array Name as Function Parameter.mp4

返回《C Programming》慕课在线视频课程列表

8.4 Using Array Name as Function Parameter.mp4在线视频

下一节:【Source program】Store array elements in reverse order.

返回《C Programming》慕课在线视频列表

8.4 Using Array Name as Function Parameter.mp4课程教案、知识点、字幕

大家好

我是云南大学信息学院丁海燕老师

欢迎走进C语言程序设计课堂

今天

我们讲解用数组名作函数参数

用数组名作函数参数时

因为实参数组名

代表该数组首元素的地址

形参应该是一个指针变量

C编译

都是将形参数组名

作为指针变量来处理的

来看一个程序

{ void fun(int arr[],int n]);

int array[10]; 

fun (array,10);调用函数 fun

fun(int arr[ ],int n)

在程序编译时是将arr

按指针变量处理的

相当于将函数fun的首部写成

fun(int *arr,int n)

以上两种写法是等价的

在该函数被调用时

系统会在fun函数中

建立一个指针变量arr

用来存放

从主调函数传递过来的

实参数组首元素的地址

当arr

接收了实参数组的首元素地址后

arr就指向实参数组首元素

也就是指向array[0]

因此

*arr就是array[0]

arr+1指向array[1]

arr+2指向array[2]

arr+3指向array[3]

也就是说

*(arr+1)

*(arr+2)

*(arr+3)

分别是array[1]

array[2]

array[3]

*(arr+i)

和arr[i]是无条件等价的

实参数组名

是指针常量

但形参数组名

是按指针变量处理

在函数调用进行虚实结合后

形参数组名(指针变量)的值

就是实参数组首元素的地址

在函数执行期间

形参数组名可以再被赋值

void fun (arr[ ],int n)

arr=arr+3

改变指针的位置

printf(″%d ″, *arr);

输出数组元素

下面来看例8.8

将数组a中n个整数按相反顺序存放

解题思路

将a[0]与a[n-1]对换

以此类推

将a[4]与a[5]对换

如图所示

3与2交换

7与4交换

9与5交换

11与7交换

0与6交换

程序如下

{ void inv(int x[ ],int n);

对函数进行声明

int i, a[10]并进行初始化

循环输出a[i]的值

inv(a,10); 调用函数

循环10次

输出a[i]的值

函数inv

程序如下

{ int temp,i,j,m=(n-1)/2;

for(i=0;i<=m;i++)

{ j=n-1-i;

temp=x[i];x[i]=x[j];x[j]=temp; }}

函数inv可优化为

{ int temp,*i,*j;

i=x;

j=x+n-1;

for( ; i

首尾两个元素依次交换

{ temp=*i; *i=*j; *j=temp; }}

规纳分析

若有一个实参数组

想在函数中改变此数组元素的值

实参与形参的对应关系有4种情况

形参和实参都用数组名

例如int a[10]

调用f(a,10);

f(int x[],int n)

实参用数组名

形参用指针变量

int a[10]

函数调用f(a,10)

f(int *x,int n)

实参形参都用指针变量

{int a[10] ,*p;

p=a;

f(p,10);
p=a;

f(p,10);

f(int *x,int n)

实参为指针变量

形参为数组名

{int a[10] ,*p;

p=a;

f(p,10);

f(int x[],int n)

下面来看例8.9

改写例8.8

用指针变量作实参

程序如下

{ void inv(int *x,int n);

int i, arr[10]

*p=arr

给p的赋值不可少

for(i=0;i<10;i++,p++)

scanf(“%d”,p);

inv(p,10)

用指针变量p作实参

for(p=arr;p

printf(“%d ”,*p);

定义inv函数

用指针变量作形参

void inv(int *x,int n)

形参x是指针变量

{ int *p,m,temp,*i,*j;

m=(n-1)/2;

i=x;

j=x+n-1;

p=x+m;

for( ; i<=p; i++,j--)

{ temp=*i; *i=*j; *j=temp; }

下面来看例8.10

用指针方法对10个整数

按由大到小顺序排序

解题思路

在主函数中

定义数组a存放10个整数

定义int *型指针变量p指向a[0]

定义函数sort

使数组a中的元素

按由大到小的顺序排列

在主函数中调用sort函数

用指针p作实参

用选择法进行排序

程序如下

{ void sort(int x[ ],int n);

对函数进行声明

int i,*p,a[10];

p=a;

通过循环输入10个数

p=a;

sort(p,10);

函数调用

for(p=a,i=0;i<10;i++)

{ printf(“%d ”,*p); p++; }

输出排序后的数

选择法降序排序函数sort

程序如下

void sort(int x[],int n)

等价于void sort(int *x,int n)

{ int i,j,k,t;

for(i=0;i

n个数需要做n-1趟排序

{ k=i;

for(j=i+1;j

if(x[j]>x[k]) k=j;

等价于if (*(x+j)>*(x+k))

k=j;

if(k!=i)

则交换x[i]与x[k]

这个语句

等价于下面这个语句

程序运行结果如下所示

用指针方法

对十个整数

按由大到小

顺序排序

输入以下程序

声明函数sort

定义指针变量p

指向数组a

p指向数组a

输入10个数

p重新指向数组a

函数调用

指针变量做实参

输出排序后的10个数

函数定义

数组作形参

用二重循环

实现选择法降序 排序

编译

连接

运行

输入12

34

5

689

-43

56

-21

0

24

65

输出降序排序后的十个数

好了

同学们

用数组名作函数参数

我们就学习到这儿

下节课再见

C Programming课程列表:

Chapter 1 Introduction

-1.1 The development and characteristics of C language

--1.The development and characteristics of C language .mp4

--1.1 Self test questions

-1.2 A simple C Language program

--1.2 A simple C Language program.mp4

--Discussion unit

--【Source program】 Example 1.1 output a line of text Hello, world! "

--【Source program】 Example 1.2 program composed of multiple functions, find the larger of two integers

--1.2 Self test questions

-1.3 Program, Programming Language and C Program Running Steps

--1.3 Program, Programming Language and C Program Running Steps.mp4

--Discussion unit

--Running steps of C source program in CodeBlocks

--1.3 Self test questions

-Course references

-chapter 1 Self-Test

Chapter 2 Algorithm

-2.1 The Concept and Description of Algorithms

--2.1 The Concept and Description of Algorithms.mp4

--Discussion unit

--2.1 Self test questions

-2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming

--2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming.mp4

--Discussion unit

--2.2 Self test questions

-Chapter 2 Self test questions

Chapter 3 Programming in C

-3.1 Simple Structure and Identifier of C Language Program

--3.1 Simple Structure and Identifier of C Language Program.mp4

--【Source program】 Example 3.1 input two integers and output the sum of two numbers.

--【Source program】 Example 3.2 input two integers and output the average value.

--【Source program】 Example 3.3 output the value of the character variable.

--3.1 Self test questions

-3.2 Constants, Variables and Assignments

--3.2 Constants, Variables and Assignments.mp4

--Discussion unit

--3.2 Self test questions

-3.3 Arithmetic, assignment, increment and decrement operators

--3.3 Arithmetic, assignment, increment and decrement operators.mp4

--3.3 Self test questions

-3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data

--3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data

--【Source program】 example of sizeof

--3.4 Self test questions

-3.5 Input and Output Examples and Character Input and Output

--3.5 Input and Output Examples and Character Input and Output.mp4

--【Source program】 Example. Find the root of quadratic equation of one variable.

--【Source program】 Example 1. Successively output three characters of BOY.

--【Source program】Example 2. Input 3 characters of BOY from the keyboard and output them to the screen

--3.5 Self test questions

-3.6 Formatted output printf function

--3.6 Formatted output printf function.mp4

--【Source program】 Example 3.4 output of integer data.

--【Source program】 Example 3.5 real data output.

--【Source program】 Example 3.6 character data output.

--【Source program】 Example 3.7 uses %s to output a string.

--3.6 Self test questions

-3.7 Formatted input scanf function

--3.7 Formatted input scanf function.mp4

--Discussion unit

--【Source program】 Example 3.8 Input and output integer data

--【Source program】Example 3.9 Input and output single precision and double precision real data.

--【Source program】Example 3.10 input and output character data.

--【Source program】Example 3.11 input and output string.

--3.7 Self test questions

-3.8 Basic Data Types of C Language

--3.8 Basic Data Types of C Language.mp4

--3.8 Self test questions

-Chapter 3 Self test questions

-Operator and expression self test questions

Chapter 4 Selection Structure

-4.1 Relational operators, logical operators, and if statements

--4.1 Relational operators, logical operators, and if statements.mp4

--Discussion unit

--【source example】Example 4.1 Find the root of the quadratic equation of one variable.

--【Source program】Example 4.2 Input two real numbers and output them from small to large.

--4.1 Self test questions

-4.2 Switch Statement

--4.2 Switch Statement .mp4

--Discussion unit

--【Source program】Example 4.3 uses switch statement to implement simple menu program

--【Source program】Example 4.4 converts centesimal score into the corresponding grade system score.

--4.2 Self test questions

-4.3 Examples of Selection Structural Programs

--4.3 Examples of Selection Structural Programs.mp4

--【Source program】 Example 4.5 determines whether a year is a leap year.

--【Source program】 Example 4.6 find the solution of quadratic equation of one variable.

--【Source program】Example 4.7 calculate the transportation cost for the user.

--4.3 Self test questions

-Chapter 4 Self test questions

Chapter 5 Loop Structure

-5.1 While and Do-While Statement

--5.1 While and Do…while statement.mp4

--Discussion unit

--【Source program】example 5.1 Use while statement to find 1 + 2 + 3 + +100

--【Source program】example 5.2 using do while statement to find 1 + 2 + 3 + +100

--5.1 Self test questions

-5.2 For Statement

--5.2 for statement.mp4

--【Source program】example 5.3 Use for loop to find 1 + 2 + 3 +100

--5.2 Self test questions

-5.3 Change the State of Loop Execution and Nested Loop

--5.3 Change the state of loop execution and nested loop .mp4

--【Source program】example 5.4 collecting charity donations

--【Source program】example 5.5 output a number between 100 and 200 that cannot be divided by 3.

--【Source program】multiplication table

--【Source program】example 5.6 Output the following 4*5 matrix.

--5.3 Self test questions

-5.4 Loop Structure Program example 1

--5.4 Loop structure program example 1.mp4

--【Source program】example 1

--【Source program】example 2

--【Source program】example 3

--【Source program】 Example 4

--5.4 Self test questions

-5.5 Loop Structure Program example 2

--5.5 Loop structure program example 2.mp4

--【Source program】example 5. Output the following figure.

--【Source program】example6. The problem of 100 chickens and 100 coins

--【Source program】example 7. Find the approximate value of PI

--5.5 Self test questions

-Chapter 5 Self test questions

Chapter 6 Batch Data Processing with Array

-6.1 Definition, Reference and Initialization of One-Dimensional Arrays

--6.1 Definition, Reference of One-Dimensional Arrays.mp4

--Discussion unit

--6.1 Self test questions

-6.2 One-Dimensional Array Programming

--6.2 One-Dimensional Array Programming.mp4

--【Source program】example 1. Fibonacci sequence

--【Source program】example 2. Find the maximum value and the minimum value.

--【Source program】example 3 exchanging array elements in reverse order.

--【Source program】example 4. Bubble sorting.

--6.2 Self test questions

-6.3 Definition, Reference and Initialization of Two-Dimensional Arrays

--6.3 Definition, Reference of Two-Dimensional Arrays.mp4

--6.3 Self test questions

-6.4 Two-Dimensional Array Programming

--6.4 Two-Dimensional Array Programming.mp4

--【Source program】example 1. Get the average score of each subject.

--【Source program】Example 2. The elements of row and column of a 2D array are interchanged

--6.4 Self test questions

-6.5 Definition, initialization and input and output of character arrays

--6.5 Definition, initialization and input and output of character arrays .mp4

--Discussion unit

--【Source program】Example 6.6 Output a known string.

--【Source program】Example 6.7 Output a diamond.

--【Source program】Example 6.10 Sorting of strings.

--6.5 Self test questions

-6.6 String Processing Function

--6.6 String Processing Function.mp4

--6.6 Self test questions

-6.7 Character Array Programming

--6.7 Character Array Programming.mp4

--6.7 Self test questions

-Chapter 6 Self test questions

Chapter 7 Modular Programming with Functions

-7.1 Function Concept and How to Define and Call Functions

--7.1 Function Concept and How to Define and Call Functions.mp4

--【Source program】Example 7.1

--7.1 Self test questions

-7.2 Data Transfer in Function Call, Call Procedure and Function Return Value

--7.2 Data Transfer in Function Call, Call Procedure and Function Return Value.mp4

--Discussion unit

--【Source program】Example 7.2

--7.2 Self test questions

-7.3 Declarations of called functions and nested calls to functions

--7.3 Declarations of called functions and nested calls to functions .mp4

--【Source program】Example 7.4 use a function to find the sum of t

--7.3 Self test questions

-7.4 Recursive call to function

--7.4 Recursive call to function.mp4

--【Source program】Example 7.6. How old is the fifth student?

--【Source program】Example 7.7 Use recursion method to find n!

--7.4 Self test questions

-7.5 Array as function parameter (1)

--7.5 Array as function parameter (1).mp4

--Discussion unit

--【Source program】Example 7.10

--7.5 Self test questions

-7.6 Array as function parameter (2)

--7.6 Array as function parameter (2).mp4

--【Source program】Selection sorting.

--【Source program】Example 7.13 find the maximum of a 3×4 matrix.

--7.6 Self test questions

-7.7 Local and global variables, internal and external functions

--7.7 Local and global variables, internal and external functions.mp4

--【Source program】Example 7.14

--7.7 Self test questions

-7.8 The Survival Period of Variables and the Storage Mode of Local Variables

--7.8 The Survival Period of Variables and the Storage Mode of Local Variables.mp4

--【Source program】Example 7.17

--7.8 Self test questions

-7.9 Storage Categories of Global Variables

--7.9 Storage Categories of Global Variables.mp4

--7.9 Self test questions

-Chapter 7 Self test questions

Chapter 8 Pointer

-8.1 Pointer Concept, Definition and Reference of Pointer Variables

--8.1 Pointer Concept, Definition and Reference of Pointer Variables.mp4

--【Source program】Example 8.1

--8.1 Self test questions

--Discussion unit

-8.2 Pointer variables as function parameters

--8.2 Pointer variables as function parameters.mp4

--Discussion unit

--【Source program】Example 8.3. Exchange two data with function call.

--8.2 Self test questions

-8.3 Pointer of array element, operation of pointer and reference of array element by pointer

--8.3 Pointer of array element, operation of pointer and reference of array element by pointer.mp4

--【Source program】Point to array elements with pointer variables.

--8.3 Self test questions

-8.4 Using Array Name as Function Parameter

--8.4 Using Array Name as Function Parameter.mp4

--【Source program】Store array elements in reverse order.

--8.4 Self test questions

-8.5 Reference to multidimensional arrays by pointers

--8.5 Reference to multidimensional arrays by pointers.mp4

--【Source program】Pointer variable pointing to one-dimensional array.

--8.5 Self test questions

-8.6 Referencing strings through pointers

--8.6 Referencing strings through pointers.mp4

--【Source program】Reference string by pointer.

--8.6 Self test questions

-8.7 Character pointer as function parameter

--8.7 Character pointer as function parameter.mp4

--【Source program】Character pointer as function parameter.

--8.7 Self test questions

-8.8 Pointer pointing to function

--8.8 Pointer pointing to function.mp4

--【Source program】Pointer variable pointing to the function.

--8.8 Self test questions

-8.9 Functions that return pointer values

--8.9 Functions that return pointer values.mp4

--【Source program】Intercept substring.

--8.9 Self test questions

-8.10 Pointer arrays and multiple pointers

--8.10 Pointer arrays and multiple pointers.mp4

--【Source program】Sorting of strings.

--8.10 Self test questions

-8.11 Dynamic memory allocation and pointer variables pointing to it

--8.11 Dynamic memory allocation and pointer variables pointing to it.mp4

--Discussion unit

--【Source program】Dynamic memory allocation.

--8.11 Self test questions

-Chapter 8 Self test questions

Chapter 9 Structure

-9.1 Define and use structural variables

--9.1 Define and use structural variables.mp4

--【Source program】Structure variable

--9.1 Self test questions

-9.2 Using structure arrays

--9.2 Using structure arrays.mp4

--Discussion unit

--【Source program】Structure array

--9.2 Self test questions

-9.3 Structure pointer

--9.3 Structure pointer.mp4

--【Source program】Structure pointer

--9.3 Self test questions

-Chapter 9 Self test questions

CodeBlocks Baidu online disk download

-CodeBlocks Baidu online disk download address

Final Exam

-Final Exam

--final exam

8.4 Using Array Name as Function Parameter.mp4笔记与讨论

也许你还感兴趣的课程:

© 柠檬大学-慕课导航 课程版权归原始院校所有,
本网站仅通过互联网进行慕课课程索引,不提供在线课程学习和视频,请同学们点击报名到课程提供网站进行学习。