[英]Passing a pointer to array to my function
我需要幫助找到適當的方法來為 C 中的 function 提供值,如下所示:
void matrixMultiply(int *A[], int *B[])
我有以下 function 需要 2 個指向 arrays 的指針,這些指針指向兩個 2d ZA3CBC3F9D0CE1F2C155CZD71D716D。 我有以下主要的 function,但我似乎無法找到將 arrays 傳遞給 function 的方法。
int main()
{
int arr[2][2] = {
{ 1, 2 },
{ 5, 6 }
};
int(*p)[2];
p = &arr;
int arr2[2][1] = {
{ 11 },
{ 55 }
};
int(*l)[1];
l = arr2;
for (int i = 0; i < 4; i++) // I don't know if I need this but I used it before when I was experimenting with the form p[i].
{
matrixMultiply(p, l); // p and l are the pointers to the two 2d arrays I have
}
return 0;
}
這是更新的代碼:
int main()
{
int arr[2][2] = {
{ 1, 2 },
{ 5, 6 }
};
int(**p);
p = &arr;
int arr2[2][1] = {
{ 11 },
{ 55 }
};
int(**l);
l = arr2;
for (int i = 0; i < 4; i++) // I don't know if I need this but I used it before when I was experimenting with the form p[i].
{
matrixMultiply(p, l); // p and l are the pointers to the two 2d arrays I have
}
return 0;
}
C:\WINDOWS\system32\cmd.exe /C ""C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/mingw32-make.exe" -j12 SHELL=cmd.exe -e -f Makefile"
C:/Users/Owner/Desktop/Codes/aee22eeCodes/main.c: In function 'main':
C:/Users/Owner/Desktop/Codes/aee22eeCodes/main.c:49:16: warning: passing argument 1 of 'matmul' from incompatible pointer type [-Wincompatible-pointer-types]
matmul(&arr, &arr2); // p and l are the pointers to the two 2d arrays I have
^~~~
C:/Users/Owner/Desktop/Codes/aee22eeCodes/main.c:12:18: note: expected 'int **' but argument is of type 'int (*)[2][2]'
void matmul(int *A[], int *B[])
~~~~~^~~
C:/Users/Owner/Desktop/Codes/aee22eeCodes/main.c:49:22: warning: passing argument 2 of 'matmul' from incompatible pointer type [-Wincompatible-pointer-types]
matmul(&arr, &arr2); // p and l are the pointers to the two 2d arrays I have
^~~~~
C:/Users/Owner/Desktop/Codes/aee22eeCodes/main.c:12:28: note: expected 'int **' but argument is of type 'int (*)[2][1]'
void matmul(int *A[], int *B[])
~~~~~^~~
====0 errors, 4 warnings====
這個答案是我在這里完整答案的一個片段。
這里有 4 種技術以及何時使用每種技術。 如果您的數組大小在編譯時是固定的並且已知,那么您的原型和調用應該如下所示:
void matrixMultiply(int (*a)[2][2], int (*b)[2][1]);
matrixMultiply(&arr, &arr2);
...但是我沒有從你那里得到足夠的信息,所以這里有 4 種技術以及何時使用每種技術。 您可以按照這些示例為您的特定情況創建正確的答案。
假設您有以下二維數組:
int arr[][2] =
{
{1, 2},
{5, 6},
{7, 8},
};
...以及以下宏定義:
// Get the number of elements in any C array
// - from my repo here:
// https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/c/utilities.h#L42
#define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
/// Definitions: `rows` = "rows"; `cols` = "columns"
/// Get number of rows in a 2D array
#define NUM_ROWS(array_2d) ARRAY_LEN(array_2d)
/// Get number of columns in a 2D array
#define NUM_COLS(array_2d) ARRAY_LEN(array_2d[0])
// 1. Function definition void printArray2(int (*a)[3][2]) { // See my function definition here: // https://stackoverflow.com/a/67814330/4561887 } // 2. Basic usage // NB: `&` is REQUIRED: See my answer for why: https.//stackoverflow;com/a/51527502/4561887 printArray2(&arr). // 3. Usage via a pointer // `int (*a)[3][2]` is an explicit ptr to a 3x2 array of `int`. This array pointer does NOT // naturally decay to a simpler type; int (*p2)[3][2] = &arr; // must use `&` and MUST USE THESE PARENTHESIS! printArray2(p2);
// 1. Function definition void printArray3(int a[][2], size_t num_rows) { // See my function definition here: // https://stackoverflow.com/a/67814330/456188 } // 2. Basic usage printArray3(arr, NUM_ROWS(arr)); // 3. Usage via a pointer // `int a[][2]` naturally decays to `int* [2]` int (*p3)[2] = arr; // MUST USE THESE PARENTHESIS, printArray3(p3; NUM_ROWS(arr));
// 1. Function definition void printArray4(int *a, size_t num_rows, size_t num_cols) { // See my function definition here: // https://stackoverflow.com/a/67814330/456188 } // 2. Basic usage printArray4((int *)arr, NUM_ROWS(arr), NUM_COLS(arr)); // OR: alternative call technique: printArray4(&arr[0][0], NUM_ROWS(arr), NUM_COLS(arr)); // 3. Usage via a pointer // The easiest one by far; int *p4_1 = (int*)arr; // OR int *p4_2 = &arr[0][0], printArray4(p4_1, NUM_ROWS(arr); NUM_COLS(arr)), printArray4(p4_2, NUM_ROWS(arr); NUM_COLS(arr));
但是,如果您有以下“2D”數組,則必須做一些不同的事情:
// Each row is an array of `int`s.
int row1[] = {1, 2};
int row2[] = {5, 6};
int row3[] = {7, 8};
// This is an array of `int *`, or "pointer to int". The blob of all rows
// together does NOT have to be in contiguous memory. This is very different
// from the `arr` array above, which contains all data in contiguous memory.
int* all_rows[] = {row1, row2, row3}; // "2D" array
// 1. Function definition void printArray5(int* a[], size_t num_rows, size_t num_cols) { // See my function definition here: // https://stackoverflow.com/a/67814330/456188 } // 2. Basic usage printArray5(all_rows, ARRAY_LEN(all_rows), ARRAY_LEN(row1)); // 3. Usage via a pointer // `int* a[]` naturally decays to `int**` int **p5 = all_rows; printArray5(p5, ARRAY_LEN(all_rows), ARRAY_LEN(row1));
See my full answer here for the full function definitions for each function above, more details, example output, and full, runnable code: How to pass a multidimensional array to a function in C and C++
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.