Formal parameters, arguments, main function parameters, arrays or pointers as function parameters of the knowledge in every corner of C language

catalogue

Definition and relation of formal parameter and argument

Formal parameters:

Actual parameters:

Relation between formal parameter and actual parameter:

Array as function parameter

Shape participating arguments of one-dimensional array as function

Shape participating arguments of one-dimensional array as function

Array as function parameter (in the form of pointer)

Pointer as function parameter

main function parameter problem

Definition and relation of formal parameter and argument

Formal parameters:

Parameters specified when defining a function. The parameter appearing in the function definition can be regarded as a placeholder. It has no data and can only wait until the function is called to receive the data passed in, so it is called formal parameter for short.

The memory of formal parameter variables will be allocated only when the function is called, and the memory will be released immediately after the call, so the formal parameter variables are only valid inside the function and cannot be used outside the function.

Actual parameters:

The specified parameters when calling the function. The parameters given when the function is called contain real data and will be used by the internal code of the function, so they are called actual parameters for short.

Arguments can be constants, variables, expressions, functions, etc. no matter what type of data the arguments are, they must have definite values when calling functions, so that these values can be transmitted to formal parameters. Therefore, assignment, input and other methods should be used in advance to make the arguments obtain definite values.
 

Relation between formal parameter and actual parameter:

The function of formal and actual parameters is to pass data. When a function call occurs, the value of the actual parameter will be passed to the formal parameter.

Array as function parameter


Shape participating arguments of one-dimensional array as function

int a[10];
 
void function(int a[],int n)  //Formal parameters
 
function(a,10)  //Actual parameters


Shape participating arguments of one-dimensional array as function

int a[5][10];
 
void function(int a[][10],int n)  //Formal parameters
 
function(a,5)  //Actual parameters

Array as function parameter (in the form of pointer)

Some students will have questions. Didn't it just say that arrays are function parameters? Why did it come in the form of pointers. Don't worry, we all know that array is also a pointer (that is, address), so array can also be expressed in the form of pointer. I put this part here, just as the buffer for array to do function parameters and pointer to do function parameters

We will directly use an example to compare the difference between the two forms of array as a function parameter, in order to obtain the maximum value in a one-dimensional array

/*
find_max:Find the maximum value of one-dimensional array by using array as parameters
    @a[]:One dimensional array that needs to be maximized
    @n: Number of elements of one-dimensional array
    Return value
        max: Maximum value of one-dimensional array
 */
int find_max(int a[],int n)
{
    int i;
	int max = a[0];
	for(i = 0; i < n; i++)
	{
		if(a[i] > max)
		{
			max = a[i];
		}
	}
	return max;
}

/*
find_max:Array takes the form of pointer as a parameter to find the maximum value of one-dimensional array
    @a[]:One dimensional array that needs to be maximized
    @n: Number of elements of one-dimensional array
    Return value
        max: Maximum value of one-dimensional array
 */
int find_max(int *b,int n)
{
	int i;
	int max = *b; //*b = *&b[0]= b[0]
	for(i = 0; i < n; i++)
	{
		if(*(b+i) > max)
	    {
		max = *(b+i);
	    }
	}
	return max;
}

In order to make it more difficult for you to better understand, let's use arrays as parameters in the form of pointers to input and output a two-dimensional array

void _3()
{
	int a[3][3];
	for(int i = 0;i<3;i++)
	{
		for(int j = 0;j < 3;j++)
		{
			scanf("%d",(*(a+i)+j));        //The input of two-dimensional array is equivalent to scanf("%d",a[i][j]);
		}
	}
	for(int i = 0;i<3;i++)
	{
		for(int j = 0;j < 3;j++)
		{
			printf("%d  ",*(*(a+i)+j));    //The output of two-dimensional array is equivalent to printf ("%d", a[i][j]); 
		}
		printf("\n");
	}
	printf("\n");
}

Pointer as function parameter

/*
_17:Use the pointer as a parameter to exchange two values
 */
void _17(int *p,int *q)
{
	int t;
	t = *p;
	*p = *q;
	*q = t;
 } 

int main()
{
    int a = 0,b = 1;
    _17(&a,&b);
    printf("a = %d, b = %d\n",a,b);
}

void func(int *p)
{
	*p = 250;
}

int main()
{
	int a;
	func(&a);//p = &a; 
    //typeof(p): int *
    //typeof(&a): typeof(a)* int *
	printf("%d\n",a);
}

main function parameter problem

Talking about the main function, we can certainly quickly type the sentence "int main() {}", but the main function actually has three optional parameters, as follows

int main(int argc, char *argv[], char *envp[]);

argc: is an int type, which represents the number of parameters on the command line
argv: char*[], is an array of string pointers.
argv[0]: the address of the first parameter string of the program is saved
argv[1]: the address of the second parameter string of the program is saved
        ....
argv[argc] is NULL, indicating the end of the parameter.


char* envp[], which is also a string array, is mainly used to save the variable string in the user environment and ends with NULL.
According to the provisions of C language, the value of argv[0] is the program name that starts the program.
        eq: ./a.out    ./hello  
So the value of agrc is at least one. If the value of agrc is 1, there is no command line parameter after the program name.

Let's try to print out the command line parameters entered on the terminal. Also test the third parameter

int main(int argc,char *argv[])    //Print commands entered by the terminal
{
	int i = 0;
	while(argv[i++])
	{
		printf("%s ",argv[i]);
	}
	printf("\n");
	i = 0;
	while(envp[i++])
    {
        printf("%s\n", envp[i]);
    }
    return 0;
}

Previous excellent articles:

Data types of basic knowledge of C language_ Listen to music with bars_ Blog of 0 -CSDN blog

The pointer of knowledge in every corner of C language_ Listen to music with bars_ Blog of 0 -CSDN blog

Arrays and functions of knowledge in every corner of C language_ Listen to music with bars_ Blog of 0 -CSDN blog

The pointer of knowledge in every corner of C language_ Listen to music with bars_ Blog of 0 -CSDN blog

Tags: C programming language

Posted by jjrosen on Sun, 31 Jul 2022 21:37:22 +0530