是否c + +允许作为沃拉斯功能参数 [英] Does C++ allow VLAs as function parameter

查看:181
本文介绍了是否c + +允许作为沃拉斯功能参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于code是用于生成幻方在我所使用的VLA功能


  

create_magic_square(INT N,INT magic_square [N] [N])结果
      print_magic_square(INT N,INT magic_square [N] [N])


 的#include<&stdio.h中GT;无效create_magic_square(INT N,INT magic_square [N] [N]);
无效print_magic_square(INT N,INT magic_square [N] [N]);诠释的main()
{
    INT大小;
    的printf(这个程序创建一个指定大小的幻方);
    输出(大小为1到99之间的奇数);    的printf(请输入魔方的大小:);
    scanf函数(%d个,&安培;大小);
    如果(大小%2 == 0 ||尺寸℃的大小||大于99)
    {
        的printf(输入错误!);
        返回0;
    }    INT广场[大小] [SIZE]
    的for(int i = 0; I<大小;我++)
        对于(INT J = 0; J<大小; J ++)
            广场[I] [J] = 0;    create_magic_square(大小,正方形);
    print_magic_square(大小,正方形);    返回0;
}无效create_magic_square(INT N,INT magic_square [N] [N])
{
    INT排= 0,列= N / 2;
    magic_square [行] [COL] = 1;
    而(magic_square [行] [COL]< = N * N)
   {
        INT NEW_ROW =((行 - 1)+ N)%N;
        INT new_col =((山口+ 1)+ N)%N;
        如果(magic_square [NEW_ROW] [new_col] == 0)
        {
            magic_square [NEW_ROW] [new_col] = magic_square [行] [COL] + 1;
            排= NEW_ROW;
            COL = new_col;
        }
        否则如果(magic_square [NEW_ROW] [new_col]!= 0)
        {
            magic_square [排+ 1] [COL] = magic_square [行] [COL] + 1;
            排排= + 1;
        }
    }
}无效print_magic_square(INT N,INT magic_square [N] [N])
{
    的for(int i = 0; I< N;我++)
   {
        对于(INT J = 0; J< N; J ++)
           的printf(%d个,magic_square [I] [J]);
        的printf(\\ n \\ n);
    }}

在保存扩展文件的的.cpp ,在编译它给了以下错误:

当我改变了这种扩展的 .C ,它工作得很好。结果
这背后有什么原因呢?结果
我觉得沃拉斯是不是在C ++允许的,是不是?

注意:检查有关沃拉斯作为参数此链接:结果
" [*]"功能参数


解决方案

您不能使用C数组的方式,除第一次程度必须是编译时间常数。

你可以做的是通为int * magic_square 指向一个一维n * n的数组,并使用一个简单的索引映射函数来得到线性指标细胞外。

您标记了一个问题,C ++,所以你应该知道

  INT广场[大小] [SIZE]

是无效的C ++或者,虽然它是有效的C99和一些编译器支持通过扩展。

对于C ++我建议使用的std ::矢量<&INT GT; VEC(尺寸大小*)作为持有人。

Given code is for generating a magic square in which i have used VLA for the function

create_magic_square(int n, int magic_square[n][n])
print_magic_square(int n, int magic_square[n][n])

 #include <stdio.h>

void create_magic_square(int n, int magic_square[n][n]);
void print_magic_square(int n, int magic_square[n][n]);

int main()
{
    int size;
    printf("This program creates a magic square of a specified size");
    printf("The size be an odd number between 1 and 99");

    printf("Enter the size of magic square: ");
    scanf("%d", &size);
    if(size%2 == 0 || size < 0 || size > 99)
    {
        printf("Wrong Entry!!!");
        return 0;
    }

    int square[size][size];
    for( int i = 0; i < size; i++)
        for(int j = 0; j < size; j++)
            square[i][j] = 0;

    create_magic_square(size, square);
    print_magic_square(size, square);

    return 0;   
}

void create_magic_square(int n, int magic_square[n][n])
{
    int row = 0, col = n/2;
    magic_square[row][col] = 1;
    while(magic_square[row][col] <= n*n)
   {
        int new_row = ((row - 1) + n) % n;
        int new_col = ((col + 1) + n) % n;
        if(magic_square[new_row][new_col] == 0)
        {
            magic_square[new_row][new_col] = magic_square[row][col] + 1;
            row = new_row;
            col = new_col;
        }
        else if(magic_square[new_row][new_col] != 0)
        {
            magic_square[row + 1][col] = magic_square[row][col] + 1;
            row = row + 1;
        }
    }
}

void print_magic_square(int n, int magic_square[n][n])
{
    for( int i = 0; i < n; i++)
   {
        for(int j = 0; j < n; j++)
           printf("%d   ", magic_square[i][j]);
        printf("\n\n"); 
    }   

}

when file saved with extension .cpp ,On compiling it is giving the following error:

When I changed this extension to .c, it worked fine.
What is the reason behind this?
I think VLAs are not allowed in C++, is it right?

NOTE: Check this link regarding VLAs as parameter:
"[*]" parameter of a function

解决方案

You can't use C-style array that way, all but the first extent must be compile-time constant.

What you can do is pass int *magic_square that points to a one-dimensional n*n array, and use a simple index-mapping function to get the linear index of the cell.

You tagged the question as C++, so you should know that

int square[size][size];

is not valid C++ either, though it's valid C99 and some compilers support it through extension.

For C++ I'd suggest to use std::vector<int> vec(size*size) as holder.

这篇关于是否c + +允许作为沃拉斯功能参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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