Search code examples
carrayscommandline

Filling an array with command line parameters


I need to fill an array[2][8] with some words passed as command line parameters (words should be separated with whitespace). I don't know how to connect CLP with my array. What if I have more than 17 letters?

I wrote code just for counting and printing parameters.

#define ROWS 2
#define COLUMNS 8
int main(int argc, char const *argv[])
{
  int counter;
  if(argc > 17)
  {
     printf("Too many elements!");
     return 0;
  }
  printf("Command line includes %d parameters:\n", argc - 1);
  for(counter = 1; counter < argc; counter++)
  printf("%d: %s\n", counter, argv[counter]);
  return 0;
}

EDIT : I have to use pointers. Array [2][8] must be filled with words after which we put whitespace. I think that 2x8 array is storing exactly 16 characters, including whitespaces. I don't know how to show when you exceed 16 letters limit.


Solution

  • What if I have more than 17 letters?

    argc is an abbreviation of argument count, so the statement: if(argc > 17) is in effect testing the number of command line arguments, not the count of characters in each one.

    Additionally, the argument char *argv[] ( where argv is short for argument vector ) is typed to accommodate any number of char for each command line argument.

    Filling an array with command line parameters
    If you wanted to capture the contents of your command line arguments into an array, either a dynamically allocated set of buffers, or preferably a variable length array (available from C99 on) are suitable for the task. The dimensions of the array (of either type) can be obtained using argc (the number of arguments) and strlen of argv[i] in a loop to get the longest length of all of the arguments. An example of this technique addresses your title question below.

    Example of using a VLA:

    int main(int argc, char const *argv[])
    {
      int counter;
      int len, maxLen=0;
    
      // find the longest length parameter
      for(counter = 0; counter < argc; counter++)
      {
          len = strlen(argv[counter]);
          if(len > maxLen) maxLen = len;
      }
    
      //using a variable length array, create a container for all parameters
      char array[argc][maxLen + 1];// +1 to allow room for null terminator
    
      // transfer contents of argv to VLA array
      for(counter = 0; counter < argc; counter++)   
      {
          strcpy(array[counter], argv[counter]);//copy CLPs into array
      }
    
      printf("Command line includes %d parameters:\n", argc - 1);
      for(counter = 1; counter < argc; counter++)
      printf("%d: %s\n", counter, array[counter]);
      return 0;
    }
    

    Example output for the arguments" this that and the other thing andareallylongargumenttoshowthatargvcanaccomodate.
    enter image description here

    Example output Using arguments with white space:
    "this that" and the "other thing" andareallylongargumenttoshowthatargvcanaccomodate
    enter image description here

    EDIT to address clarifications in comments.

    The following test for (and limits) max number of arguments to ROWS , and allows length limits of arguments to exceed COLUMNS-1, but trims to length if too long. If string is less than COLUMNS-1 long, pads remaining space with &. If string contains any white space, replaces it with &...

    #define ROWS 2
    #define COLUMNS 8
    int main(int argc, char const *argv[])
    {
      int counter, i;
    
      char array[ROWS][COLUMNS];
    
      if(argc > 3) 
      {
          printf("Too many arguments.  2 max.\n(Hit any character to exit.)");
          getchar();
          return 0;
      }
    
    
      for(counter = 0; counter < argc-1; counter++) 
      {   //trim to legal string length.
          strncpy(array[counter], argv[counter+1], COLUMNS-1);
          array[counter][COLUMNS-1]=0;//set last char to null
          for(i=0;i<COLUMNS-1;i++)
          {   //test for any white space or NULL 
              //character within legal string length
              if((isspace(array[counter][i])) || array[counter][i] == NULL) array[counter][i] = '&';
          }
      }
    
      printf("Command line includes %d parameters:\n", argc - 1);
      for(counter = 0; counter < argc-1; counter++)
      printf("%d: %s\n", counter, array[counter]);
      return 0;
    }
    

    Results for example parameters - echo.c "c language is ok":
    enter image description here