Having a problem with this part of my code, i declare an array of size "s" then try to assign each index of the array the atoi value of each character of argv like so

int s = strlen(argv[1]);
int key[s];

for(int a = 0; a < s; a++)
       key[a] = atoi(argv[1][a]);

but get this error

error: incompatible integer to pointer conversion passing 'char' to  parameter of type 'const char *'; take the address with & [-Werror,-Wint-conversion]
       key[a] = atoi(argv[1][a]);

what am I doing wrong?


It's a contract violation. From man atoi:

atoi, atol, atoll, atoq - convert a string to an integer

int atoi(const char *nptr);

argv[1][a] is a char not a char * (string).

Since all arguments are strings, you would use atoi when you need to convert the whole argument to a number. For example, something like "height" in the mario exercise. Instead of using a get_int() as the spec required, another approach might have been to accept the height as an argument, something like ./mario 22. In that case, one would need to convert 22 from string to int, and atoi would be an appropriate choice. Since the argument to vigenere is a string, you might have more success using "ascii arithmetic".

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .