Home > Software design >  C: problem using argv: invalid initializer
C: problem using argv: invalid initializer

Time:11-29

does anyone know why it throws following error when trying to run:

decrypt.c: In function 'main':
decrypt.c:14:30: error: invalid initializer
   14 |         char currentWord[] = argv[count];
      |  

It occurs in line 14. I want to pass every command line parameter into the char[] current word, but somehow it doesn't work.

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

int main (int argc, char* argv[]) {

    for (int count = 1; count <= argc; count  ) {

        char tmp1;
        char tmp2;
        int countCurrent = 0;

        char currentWord[] = argv[count];

        int wordLength = strlen(currentWord);
        int amountOfLetters = wordLength/2;

        for (int i = 0; i < amountOfLetters; i  ){

            tmp1 = currentWord[countCurrent];
              countCurrent;
            printf("Das ist der %i. Wert: %c\n",countCurrent, tmp1);

            tmp2 = currentWord[countCurrent];
              countCurrent;
            printf("Das ist der %i. Wert: %c\n",countCurrent, tmp2);
        }

        countCurrent = 0;
    }
}

Thank you for any help :)

Best Enno

CodePudding user response:

You can't initialize an array this way.

You need to:

char currentWord[strlen(argv[count])   1];
strcpy(currentWord, argv[count]);

Indexes are from zero in C language

for (int count = 0; count < argc; count  ) 
  • Related