参考:C 传递数组给函数
参考:二维数组作为函数参数传递剖析(C语言)(6.19更新第5种)
总结:
一维数组参数,可以是地址、arr[]、arr[n]
二维数组比较复杂,需要提供列的值,否则会报错,可参考上面的网址
#include <stdio.h> #include <stdlib.h> void myFunc1(int length, int *arr) { for (int i = 0; i < length; i++) { printf("%d ", arr[i]); } printf("\n"); return; } void myFunc2(int length, int arr[]) { for (int i = 0; i < length; i++) { printf("%d ", arr[i]); } printf("\n"); return; } void myFunc3(int length, int arr[length]) { for (int i = 0; i < length; i++) { printf("%d ", arr[i]); } printf("\n"); return; } void myFunc4(int row, int column, int *arr2) { for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { printf("arr2[%d][%d] = %d ", i, j, *(arr2 + i*column + j)); } printf("\n"); } return; } void myFunc5(int row, int column, int arr2[][column]) { for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { printf("arr2[%d][%d] = %d ", i, j, arr2[i][j]); } printf("\n"); } return; } int main() { int arr[5] = {1, 2, 3, 4, 5}; printf("Parameter is pointer:\n"); myFunc1(5, &arr[0]); // ok with arr printf("Parameter is array[]:\n"); myFunc2(5, &arr[0]); // ok with arr printf("Parameter is array[length]:\n"); myFunc3(5, &arr[0]); // ok with arr int arr2[2][3] = {{1, 2, 3}, {4, 5, 6}}; printf("Parameter is pointer:\n"); myFunc4(2, 3, arr2[0]); // &arr2[0][0] is ok, but arr2 is not ok // *arr2 is ok. // but actually they have the same address printf("Parameter is pointer:\n"); myFunc5(2, 3, arr2); // not ok with arr2[0] // not ok with &arr2[0][0] printf("\narr2 = %p\n", arr2); printf("arr2[0] = %p\n", arr2[0]); printf("*arr2 = arr2[0] = %p\n", *arr2); printf("arr2[0][0] = %p\n", &arr2[0][0]); printf("**arr2 = *arr2[0] = arr2[0][0] = %p\n\n", &arr2[0][0]); printf("address of this two-dimentional array:\n"); printf("arr2 = %p\n", arr2); for (int i = 0; i < 2; i++) { printf(" arr2[%d] = %p\n", i, arr2[0]); for (int j = 0; j < 3; j++) { printf(" arr2[%d][%d] = %p ", i, j, &arr2[i][j]); } printf("\n"); } printf("we can use the unexisted element, interesting!\n"); printf("*arr2 means arr2[0], the first row.\n"); printf("**arr2 means *arr2[0] means arr2[0][0], the first element of the first row.\n"); printf("arr2[1][0] = %d\n", arr2[1][0]); printf("arr2[0][3] = %d\n", arr2[0][3]); printf("*(*(arr2 + 1) + 0) = %d\n", *(*(arr2 + 1) + 0)); printf("*(*(arr2 + 0) + 3) = %d\n", *(*(arr2 + 0) + 3)); printf("*(arr2[0] + 1*3 + 0) = %d\n", *(arr2[0] + 1*3 + 0)); printf("*(*arr2 + 1*3 + 0) = %d\n", *(*arr2 + 1*3 + 0)); printf("*(&arr2[0][0] + 1*3 + 0) = %d\n", *(&arr2[0][0] + 1*3 + 0)); printf("*(&arr2[0][0] + 3) = %d\n", *(&arr2[0][0] + 3)); return 0; }
output:
Parameter is pointer: 1 2 3 4 5 Parameter is array[]: 1 2 3 4 5 Parameter is array[length]: 1 2 3 4 5 Parameter is pointer: arr2[0][0] = 1 arr2[0][1] = 2 arr2[0][2] = 3 arr2[1][0] = 4 arr2[1][1] = 5 arr2[1][2] = 6 Parameter is pointer: arr2[0][0] = 1 arr2[0][1] = 2 arr2[0][2] = 3 arr2[1][0] = 4 arr2[1][1] = 5 arr2[1][2] = 6 arr2 = 0x7ffc6bdc38a0 arr2[0] = 0x7ffc6bdc38a0 *arr2 = arr2[0] = 0x7ffc6bdc38a0 arr2[0][0] = 0x7ffc6bdc38a0 **arr2 = *arr2[0] = arr2[0][0] = 0x7ffc6bdc38a0 address of this two-dimentional array: arr2 = 0x7ffc6bdc38a0 arr2[0] = 0x7ffc6bdc38a0 arr2[0][0] = 0x7ffc6bdc38a0 arr2[0][1] = 0x7ffc6bdc38a4 arr2[0][2] = 0x7ffc6bdc38a8 arr2[1] = 0x7ffc6bdc38a0 arr2[1][0] = 0x7ffc6bdc38ac arr2[1][1] = 0x7ffc6bdc38b0 arr2[1][2] = 0x7ffc6bdc38b4 we can use the unexisted element, interesting! *arr2 means arr2[0], the first row. **arr2 means *arr2[0] means arr2[0][0], the first element of the first row. arr2[1][0] = 4 arr2[0][3] = 4 *(*(arr2 + 1) + 0) = 4 *(*(arr2 + 0) + 3) = 4 *(arr2[0] + 1*3 + 0) = 4 *(*arr2 + 1*3 + 0) = 4 *(&arr2[0][0] + 1*3 + 0) = 4 *(&arr2[0][0] + 3) = 4
原文地址:https://www.cnblogs.com/alex-bn-lee/p/11236873.html
时间: 2024-11-02 21:21:06