[C language] Pointer advanced fourth station, parameter transmission of array / pointer


Friends, here we are!

Pointer advanced fourth station 🚏, Parameter transmission problem

0. Cited examples

The type of formal parameter in the user-defined function should correspond to the actual parameter type passed in the function call

In the study of elementary C language, we know that parameters can be divided into two types: address transmission and value transmission

Let's review it 👉 [link]

The test function is of type int, and the parameter a we passed is also of type int

void test(int n)
{}
int main()
{
    int a=1
    test(a);
    return 0;
}

Well, now you know the basic concept of function parameter passing 😁

Then let's analyze the code of array parameter passing and pointer parameter passing!

1. One dimensional array parameter transfer

Take a look at the code example of the following function

Who do you think is right and who is wrong?

include <stdio.h>
void test(int arr[])
{}
void test(int arr[10])
{}
void test(int *arr)
{}
void test2(int *arr[20])
{}
void test2(int **arr)
{}
int main()
{
	int arr[10] = {0};
	int *arr2[20] = {0};
	test(arr);
	test2(arr2);
    return 0;
}
  • The NO.1 parameter is received in the form of an array, which is correct

  • NO.2 is also in the form of array, correct

    It should be noted that array parameter passing does not open up a new array

    So the number in function [] doesn't matter

  • NO.3 array name is the address of the first element. It is received with a pointer. It is correct

  • NO.4 arr2 is a pointer array, (int *arr[20]) corresponds to the original array, correct

  • The NO.5 array name is the address of the first element, and the first element of arr2 is an int * type

    You can use the secondary pointer to receive, correct!

2. Two dimensional array parameter transfer

After understanding one-dimensional arrays, let's take a look at two-dimensional arrays

Which of the following functions are right and wrong?

void test(int arr[3][5])//One by one, correct!
{}
void test(int arr[][])//Omit column, error!
{}
void test(int arr[][5])//Lines can be omitted, correct!
{}
void test(int *arr)//The first element of a two-dimensional array is the first row
{}//The first line is of type int(*)[5]. Error!
void test(int* arr[5])//Pointer array, error!
{}
void test(int (*arr)[5])//Array pointer, correct!
{}
void test(int **arr)//arr is not the address of the first level pointer, error!
{}

int main()
{
	int arr[3][5] = {0};
    test(arr);
    return 0;
}

According to the code of two-dimensional array parameters, review the following knowledge points

  • The first element of a two-dimensional array is the first row
  • When defining a two-dimensional array, you can omit rows, not columns

The type of the first line is int[5], which should be put in Array pointer Inside!

3. Primary pointer transfer parameter

#include <stdio.h>
void print(int *p, int sz)//Accept with int *
{
 int i = 0;
 for(i=0; i<sz; i++)
 {
 printf("%d\n", *(p+i));
 }
}
int main()
{
 int arr[10] = {1,2,3,4,5,6,7,8,9};
 int *p = arr;
 int sz = sizeof(arr)/sizeof(arr[0]);
 
 print(p, sz);//First level pointer p, passed to function
 return 0;
}

4. Secondary pointer transfer parameter

What is a secondary pointer? 👉 Point me

The following is the basic form of secondary pointer parameter transfer

pp is of type int * *, and & P is the address of the pointer variable, which should be received with a secondary pointer

#include <stdio.h>
void test(int** ptr)
{
 	printf("num = %d\n", **ptr); 
}
int main()
{
 	int n = 10;
 	int*p = &n;
 	int **pp = &p;
	test(pp);
	test(&p);
	return 0;
}

Let's look at the following code

&p. The types of pp and arr parameters are secondary pointers

The test function should use char * * to receive

void test(char** p)
{}

int main()
{
	char ch = 'w';
	char* p = &ch;
	char** pp = &p;
	char* arr[5];

	test(&p);
	test(pp);
	test(arr);
	
	return 0;
}

arr is the array name of the pointer array, and the array name is the address of the first element of the array

The first element of the array is of type char *, so it should be received with a secondary pointer

5. How to judge the parameter type

If we are writing code, we should use what type to receive it

You can write a test code, and the VS compiler will report the error "inconsistent type"

epilogue

The problem of parameter transmission is not very difficult, but we still have to master it

In this way, we can avoid the bug of different parameter types when writing custom functions!

Stand firm and drive to the next station immediately 🚌: Function pointer

Keywords: C Back-end

Added by dunhamcd on Mon, 24 Jan 2022 02:17:21 +0200