【问题标题】:Pointers in C, returning from functions?C中的指针,从函数返回?
【发布时间】:2011-11-07 19:43:14
【问题描述】:

我正在尝试用 C 创建一个简单的矩阵乘法程序。 为此,我选择使用 2 个函数:1 用于简单地创建矩阵,1 用于保存所有创建的矩阵(用于进行乘法运算)。

我终于设法弄清楚如何从函数传递数组(或者更具体地说,从函数传递指向数组的指针),但是,我希望下面的函数 matrixWelcome() 传递行数& 数组的列。这就是我卡住的地方。

目前 gcc 给我错误无效类型参数的“一元 *”行 -> 行 = *i;

我的代码是:

void matrixWelcome() {
    int selection;
    int a, b, c, d, *rows, *columns;
    int *matrix1[0][0], *matrix2[0][0];

    printf("Welcome to matrix mode\n");
    printf("Please select from the following: \n");
    printf("1 - Matrix multiplication");

    scanf("%d", &selection);

    switch (selection) {
        case 1:
            printf("Selected matrix multiplication...\n");
            printf("Please enter matrix 1\n");

            matrix1[*rows][*columns] = matrixInput(*rows, *columns);
            printf("%d\n", *matrix1[1][1]);
            a = *rows;
            b = *columns;

            printf("****ROWS = %d, COLUMNS = %d", a, b);

           // printf("Matrix 1 has %d rows and %d columns", rows, columns);
            printf("Please enter matrix 2\n");
           // matrix2 = matrixInput();
            break;

        default:
            printf("No input entered\n");
            break;
    }
}

int *matrixInput(int *rows, int *columns) {
    int i, j, x, y;
    int *array_pointer = malloc(5 * sizeof(int)); //grab some memory

    if (array_pointer == NULL) { // check that we have successfully got memory
        return NULL;
    }

    // Number of rows & columns for matrix 1
    printf("Enter number of rows: ");
    scanf("%d", &i);
    printf("\nEnter number of columns: ");
    scanf("%d", &j);

    rows = *i;
    columns = *j;

    // Initialise 2D array to hold values
    int matrix_input[i][j];

    printf("Enter values for matrix, starting at 1,1 and moving across 1st row; then move across 2nd row etc..");

    // loop to store values in matrix
    for (x=0; x<i; x++) {
        for (y=0; y<j; y++) {

            int value_entered;
            scanf("%d", &value_entered);
            matrix_input[x][y] = value_entered; 


        }
    }

    *array_pointer = matrix_input[i][j]; 

    // print out matrix - just to confirm
    printf("You've entered the following matrix: \n");
    for (x=0; x<i; x++) {
        for (y=0; y<j; y++) {
            printf("%4d", matrix_input[x][y]);
        }
        printf("\n");
    }
    return array_pointer;
}

任何帮助将不胜感激。

【问题讨论】:

  • w.r.t.标题:请记住,指向对象的指针仅在该对象的生命周期内有效。 (例如,一旦函数返回,指向函数中声明的数组的指针就无效了。)

标签: c pointers


【解决方案1】:

i 是一个int。你不需要取消引用它。您确实需要取消引用 rows,不过 - *rows = i 可以正常工作。但是,在调用你的函数时不要取消引用。使用:

 matrixInput(rows, columns);

因为你想传递指针。

【讨论】:

  • 更具体地说,您不能取消对 int 的引用。它没有任何概念意义。
  • 感谢您的帮助,非常感谢。当你说你不取消引用整数时,你怎么继续取消引用行 '*rows = i' ?
  • 啊,是的,对不起,那太傻了。我现在收到分段错误:运行程序时出现 11(编译时没有任何错误或警告)。有什么建议么?谢谢
  • 您没有为rowscolumns 分配任何空间。
【解决方案2】:
int *matrix1[0][0], *matrix2[0][0];

这很奇怪且不正确(您不应该将全局变量声明为 0 维数组)。

而且您的代码不完整。你至少忘记了#include &lt;stdio.h&gt;#include &lt;stdlib.h&gt;

而且您的代码无法编译。

您应该尝试在启用所有警告的情况下进行编译,例如在 Linux 上使用 GCC

gcc -Wall -g cud.c -o cud

我收到 4 个错误和超过 10 个警告。

在没有警告和错误之前不要发布代码。编辑和改进您的代码,直到所有错误和警告都消失。然后调试您的代码(使用调试器,例如 Linux 上的 gdb)。

【讨论】:

  • 一个典型的函数返回指针的例子就是malloc(它通常完全用C编码,在mmap这样的低级系统调用之上)
  • 对不起 - 忘了提到这只是代码的一部分。我有很多其他代码,其中包括库等。不过感谢您的帮助:)
猜你喜欢
  • 2010-12-25
  • 1970-01-01
  • 2015-08-05
  • 1970-01-01
  • 2015-08-07
  • 2016-06-10
  • 1970-01-01
  • 1970-01-01
  • 2011-12-06
相关资源
最近更新 更多