Valar
Valar

Reputation: 21

Using variables to define Array Size (Codeblocks)

I used this code in "codeblocks" and it worked like a charm:

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

int main()
{
   int i,j, n, m;

   printf("Input n & m: "); 
   scanf("%d %d", &n, &m);

   int a[n][m];

   //Reading Array Values
   for(i = 0; i < n; i++)
   {
        for(j = 0; j < m; j++)
        {
            printf("a[%d][%d] = ", i, j);
            scanf("%d", &a[i][j]);
        }
   }
   //Displaying Array Values
   for(i = 0; i < n; i++)
   {
        for(j = 0; j < m; j++)
        {
            printf("%d ", a[i][j]);
        }

        printf("\n");
   }
}

But I saw some topics where people say that this is a kind of option which comes from gcc compiler actually. So I was wondering, should I use this in future or this is a wrong way? I thought not having headache with vector function and i decided to do this way and it worked very well. I guess this will work only on the version of gcc compiler I use. But anyway, is there still a problem with that or I can use it smoothly?

Upvotes: 2

Views: 1136

Answers (1)

Mahesh Revaskar
Mahesh Revaskar

Reputation: 104

I think there is no GCC compiler specifc behavior in your code. I executed the same on gcc compiler. You can check the result here:

jdoodle.com/a/biz

https://www.jdoodle.com/embed/v0/c/gcc-5.3.0/biz

<div data-pym-src="https://www.jdoodle.com/embed/v0/c/gcc-5.3.0/biz"></div>
<script src="https://www.jdoodle.com/assets/jdoodle-pym.min.js" type="text/javascript"></script>

Upvotes: 1

Related Questions