C language pointer understanding -- the usage of one dimension array as function parameter

Usage of one-dimensional array as function parameter:

  1. When a one-dimensional array is used as a function parameter, it will degenerate into a pointer
  2. The c/c + + compiler optimizes one-dimensional arrays when they are used as function parameters

                   int a[10]  ----->int a[] ---->int *a

So when an array is used as a function parameter, only pointers can be passed

3. When the function is called, the most correct way is to pass the array first address and the effective data length to the called function

 

//When the function is called, the value of the argument is mechanically passed to the parameter (c int array scenario)

On the understanding of formal parameters:

Is parameter variable written in function or variable written in function,

From the perspective of CC + + compilation, there is no difference (4-byte memory allocation);

Only parameter variables written on functions have external properties

 

Here is an example of the program

#include "stdlib.h"
#include "string.h"
#include "stdio.h"

//When an array is used as a function parameter, it will degenerate into a pointer 
int printfArray(int a[]) //Here, the parameter a[10] has the same effect as a []
{
	int i = 0;
	int num = 0;

	num = sizeof(a) / sizeof(*a);  //Here the num value is 1
	printf("num=%d.\n", num);
	for (i = 0; i < 10; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
	return 0;
}

//int a[10]  -=-->int a[] ---->int *a
//When an array is a function parameter, if an int a[10] statement is defined in the parameter,
//c/c + + compiler will do optimization, and the technical deduction is as follows
//int a[10]  -=-->int a[] ---->int *a
//Summary: when a function is called, the most correct way is to pass the array first address and effective data length to the called function
//When the function is called, the value of the argument is mechanically passed to the parameter (c int array scenario)

//About formal parameters:
/*
		Parameter variable written in function or variable written in function,
		From the perspective of CC + + compilation, there is no difference (allocating 4 bytes of memory);
		It's just a parameter variable written on a function with external properties
*/


int printfArray04(int *a, int num)
{
	int i = 0;
	for (i = 0; i < num; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
	return 0;

}


int sortArray(int a[])
{
	int i = 0, j = 0;
	int tmp;
	for (i = 0; i < 10; i++) //Outer circulation
	{
		for (j = i + 1; j < 10; j++) //inner loop
		{
			if (a[i] > a[j])
			{
				tmp = a[i];
				a[i] = a[j];
				a[j] = tmp;
			}
		}
	}
	return 0;
}
void main()
{
	//Define array print array array sort
	int i = 0, j = 0;
	int num2;
	int a[10] = { 1,3,4,5,65,76,7,34,4,32 };
	int tmp;
	
	num2 = sizeof(a) / sizeof(*a); //Here, num2 is 10
	printf("num2:%d \n", num2);

	printf("Before sorting\n ");
	printfArray04(a, 10);
	sortArray(a);

	printf("After sorting\n ");
	printfArray(a);

	system("pause");
}

 

Posted on Fri, 29 Nov 2019 13:05:23 -0800 by jmgrhm