0

I was trying to pass a 2D array to a function using command-line parameters.

First I have to escape the prototype error so used int arr[][3] but could not get through the argv error which is mentioned in the screenshot here.

Is it possible to pass a 2D array the way I am trying or am I just banging my head onto the wall enter image description here

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

void twoD_array(int arr[][3]);

int main(int argc,char * argv[])
{
     if(argc == 1)
     {
        printf("command line argument fails\n");
        return 0;
     }
     int i,j;
     int arr[atoi(argv[1])][atoi(argv[2])];
     twoD_array(arr);

     printf("Your 2D array is\n");
     for(i = 0; i < atoi(argv[1]); i++)
     {
         for(j = 0;j < atoi(argv[2]); j++)
         {
             printf("%d ",arr[i][j]);
         }
             printf("\n");
     }
}

void twoD_array(int arr[atoi(argv[1])][atoi(argv[2])])
{
   int i,j;
   printf("enter elements of 2D array\n");
   for(i = 0; i < atoi(argv[1]); i++)
   {
       for(j = 0;j < atoi(argv[2]); j++)
       {
           scanf("%d",&arr[i][j]);
       }
            
  }
}
6
  • You need to pass the dimensions as two more function arguments preceding the array argument, for example void twoD_array(int y, int x, int arr[y][x]); Commented Oct 7, 2022 at 18:46
  • i dont think so bcz same model was working perfectly fine when i was just passing a 2D array to a function without commandline parameters.it will be appreciated if you could do changes in the code as you wish and show Commented Oct 7, 2022 at 18:49
  • That was for int arr[][3] which relies on the inner dimension being 3 but the outer dimension is still unknown. Commented Oct 7, 2022 at 18:50
  • ok let me try this Commented Oct 7, 2022 at 18:52
  • First store width and height to dedicated variables rather than playing with atoi(argv[xxx]) all the time Commented Oct 7, 2022 at 22:19

2 Answers 2

2

You cannot define a function like this:

void twoD_array(int arr[atoi(argv[1])][atoi(argv[2])])
{
    …
}

The variable argv is not in scope (undefined), and you can't use function calls like that in the function parameter list.

You could use VLA (variable-length array) notation, though:

void twoD_array(int rows, int cols, int arr[rows][cols])
{
    …
}

Support for this was non-existent in C90, is mandatory in C99, and is optional in C11 and C18. C23 will make VLA support in parameter lists mandatory once more, but support for local (automatic) allocation of stack VLA variables will be optional (but they can always be allocated dynamically via malloc() et al).

See C11 §6.7.6.2 Array declarators and §6.7.6.3 Function declarators (including prototypes). They are not the most easily read sections of the standard, though.

Sign up to request clarification or add additional context in comments.

Comments

0

final answer:

  #include <stdio.h>
  #include <stdlib.h>
  #include<string.h>
  void twoD_array(int arr[][3]);
  int ROW,COL;
  int main(int argc,char * argv[])
  {
       if(argc == 1)
       {
              printf("command line argument fails\n");
              return 0;
       }
       int i,j;
       int arr[atoi(argv[1])][atoi(argv[2])];
       ROW = atoi(argv[1]);
       COL = atoi(argv[2]);
       twoD_array(arr);

       printf("Your 2D array is\n");
       for(i = 0; i < atoi(argv[1]); i++)
       {
              for(j = 0;j < atoi(argv[2]); j++)
             {
                  printf("%d ",arr[i][j]);
             }
          printf("\n");
       }
   }

   void twoD_array(int arr[ROW][COL])
  {
         int i,j;

         printf("enter elements of 2D array\n");
         for(i = 0; i < ROW; i++)
         {
                for(j = 0;j < COL; j++)
                {
                   scanf("%d",&arr[i][j]);
                }
            
         }

   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.