如何在C中声明一个可变大小的数组? [英] How do I declare a variable sized array in C?

查看:225
本文介绍了如何在C中声明一个可变大小的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我正在做矩阵乘法,我需要制作一个 mxn 数组和一个 pxq 数组。 br>
但是,我不知道该怎么做。

Well, I am doing matrix multiplication and I need to make an m x n array and a p x q array.
However, I do not know how to.

这是我的程序,当我手动输入值时会打印正确的输出:

Here is my program which prints correct output when I feed in values manually:

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    /*
        Rows and columns for matrices.
    */
    int m , n; // rows and columns of the first matrix
    int p , q; // rows and columns of the second matrix

    /*
        1st matrix is a 2x3 matrix
    */
    m = 2;
    n = 3;

    /*
        2nd matrix is a 3x2 matrix
    */
    p = 3;
    q = 2;


    /*
        Create the matrices.
        Give them values.
    */
    int matrix1[m][n] = {
                            {2,3,4},
                            {5,6,7}
                        };
    int matrix2[p][q] = {
                            {1,7},
                            {3,9},
                            {5,11}
                        };

    /*
        Check if we can multiple the matrices.
        For matrix multiplication,
        the number of COLUMNS of FIRST matrix must be equal to
        the number of ROWS of SECOND matrix
    */
    if(n==p){
        /*
            Create a new matrix.
            The resulting matrix will have M rows and Q columns.
            That is, the matrix is a MxQ matrix.
        */
        int matrix3[2][2]; 

        /*
            We need three loops so we have 3 variables.
        */
        int i = 0; // iterates over matrix1 rows
        int j = 0; // iterates over matrix1 columns
        int k = 0; // iterates over matrix2 rows
        int l = 0; // iterates over matrix2 columns


        while(i < m){
            l = 0;
            while(l < q){
                int element = 0;
                while(j < n && k < p){
                    element += matrix1[i][j] * matrix2[k][l];
                    matrix3[i][l] = element;
                    j++;
                    k++;
                }
                printf("\t%d",element);
                l++;
                j = 0;
                k = 0;
            }
            printf("\n");
            i++;
        }

    }else{
        printf("Matrices can not be multiplied");
    }
}  

矩阵声明被标记为错误。 我该如何解决?

The matrix declaration gets flagged as an error. How do I solve it?

推荐答案


我该如何解决?

How do I solve it?

首先,不使用VLA。您不需要VLA来完成此特定任务。

In first place, by not using a VLA. You don't need VLAs for this particular task.

关于实际问题是什么:可变长度数组无法初始化。必须逐一分配给它们的元素,或者使用某种质量分配技术,例如 memcpy()

As to what the actual problem is: variable-length arrays cannot be initialized. You have to assign to their elements one-by-one or use some mass-"assignment" technique such as memcpy().

这篇关于如何在C中声明一个可变大小的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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