1

I just can't seem to get this right. The example in the week 2 short Arrays is as follows: int foo[5]{1,2,3,4,5}; int bar[5]

for(j=0;j<5;j++) { bar[j]=foo[j]; }

This does not seem to work with strings in my case. I have tried two methods:

int main (int argc, string argv[2]) { int keylen=strlen(argv[1]); int pk=0; string ky[keylen]; for(pk=0;p<keylen;pk++) { ky[pk]=tolower(argv[1][pk); } ...

and a little longer method where I specify that the data on the right is a char. int main (int argc, string argv[2]) { int keylen=strlen(argv[1]); int pk=0; string ky[keylen]; for(pk=0;p<keylen;pk++) { char ori=argv[1][pk]; ky[pk]=tolower(ori); } ...

I keep getting this error in both cases: incompatible integer to pointer conversion assigning to 'string' (aka 'char *') from 'int' [-Werror,-Wint-conversion] What am I doing wrong?

1 Answer 1

1

You declare ky as string[keylen], it should be char[keylen+1] (don't forget the null terminator, that's why +1).

Don't forget to check argc before accessing argv[1], it might not exist at all.

Don't add any number to argv in the main method's signature. I think it doesn't matter, but it might be misleading, as argv's length is stored in argc and not determined by any number you place there.

3
  • Why should it be char though?
    – Talim
    Commented Oct 20, 2019 at 14:43
  • What do you mean? char holds a single character. string is an alias for char*, holds a memory address of where supposedly characters are stored. A string ky[keylen] is an array of keylen of such pointers.
    – Blauelf
    Commented Oct 21, 2019 at 19:27
  • Thanks! It all became way much clearer after Lecture 3. I was doing the problem set from Week 2, so I was completely lost.
    – Talim
    Commented Oct 23, 2019 at 14:14

You must log in to answer this question.

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