将指向数组的指针传递给我的函数 [英] Passing a pointer to array to my function

查看:46
本文介绍了将指向数组的指针传递给我的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助找到合适的方法来为我的 C 函数提供值,如下所示:

I need help finding the appropriate way to provide values to my function in C which can be seen below:

void matrixMultiply(int *A[], int *B[])我有以下函数需要 2 个指向数组的指针,这些指针指向两个 2d 数组.我有以下主函数,但我似乎无法找到将数组传递给函数的方法.

void matrixMultiply(int *A[], int *B[]) I have the following function that needs 2 pointer to arrays, and those pointers point to two 2d arrays. I have the following main function, but I just can't seem to figure out a way to pass my arrays to the 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 种技术以及每种技术的使用时机.如果您的数组大小是固定的并且在编译时已知,您的原型和调用应如下所示:

Here are 4 techniques and when to use each. If your array sizes are fixed and known at compile-time, your prototype and call should look like this:

void matrixMultiply(int (*a)[2][2], int (*b)[2][1]);
matrixMultiply(&arr, &arr2);

...但是我没有从你那里得到足够的信息,所以这里有 4 种技术以及每种技术的使用时机.您可以按照这些示例为您的特定情况创建正确的答案.

...but I don't have enough information from you, so here are the 4 techniques and when to use each. You can follow these examples to create the right answer for your particular situation.

假设您有以下二维数组:

Assume you have the following 2D array:

int arr[][2] =
{
    {1, 2},
    {5, 6},
    {7, 8},
};

...以及以下宏定义:

...and the following macro definitions:

// 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. 如果二维数组每次总是相同的大小(它有固定的行数和固定的列数)(在下面的示例中为 3 行和 2 列),请执行这:
  1. If the 2D array is ALWAYS the same size each time (it has a FIXED number of rows, and a FIXED number of columns) (3 rows and 2 columns in the below example), do this:
// 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);

  • 如果二维数组的行数可变,但列数固定(在本例中为 2),做这个:

  • If the 2D array has a VARIABLE number of rows, but a FIXED number of columns (2 in this case), do this:

    // 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));
    

  • 如果二维数组具有可变行数和可变列数,请执行此操作(this方法是最通用的,通常是我处理多维数组的首选方法):

    // 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"数组,但是,您必须做一些不同的事情:

    If you have the following "2D" array, however, you must do something different:

    // 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. 如果二维数组实际上是由一堆指向其他数组的ptr组成(如上图所示),做这个:
    1. If the 2D array is actually built up of a bunch of ptrs to other arrays (as shown just above), do this:
    // 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));
    

    请在此处查看我的完整答案,了解上述每个函数的完整函数定义、更多详细信息、示例输出和完整的可运行代码:如何将多维数组传递给C和C++中的函数强>

    这篇关于将指向数组的指针传递给我的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆