0

I tried to put argv[1] into a cycle:

for (each argv[1][i])
    int k[i] = atoi(argv[1][i]); // here...

...the compiler tells me to add '&' before argv to address; afterward, the compiler tells me that variable-sized object may not be initialized. Must I initialize k and then return something to that?

  • I have not viewed this yet in the lessons, shorts or walkthroughs.

  • Assuming that I need to save each variable k, in this case k[i], obtained by iteration in a new string of int named k, how could I do this? With the code I wrote, I can see k or k[i] only inside the for loop.

  • Is there an easier way to do this?

1

atoi() transforms a string into a number, not a char. As it expects a char* it is telling you to put the & so that it can work with an address.

Also, to fill an int array, try the following:

int k[LENGTH];
for (int i = 0; i < LENGTH; i++)
{
    k[i] = number;
}

TL; DR: You put in a parameter of the wrong type.

4
  • thank you very much guys, your answers are very clear. I now understand better the errors. One more question to go ahead with the problem set: _ is strictly necessary trasormare the key in a string of integers ? OR , I can calculate the shift directly from the letters?_
    – Dot4inch
    Nov 20 '14 at 17:40
  • You can but it it's more convenient to use ints.
    – rigel
    Nov 21 '14 at 7:34
  • why is more convenient please? I'm sorry I'm very courious and excited to write source in c, so I wish to learn all I can : )
    – Dot4inch
    Nov 30 '14 at 23:02
  • Well, for one, you don't have to use ASCII math every time you wish to access the i-th member of the key, having done that conversion to a Caesar cipher like key beforehand. That way, the part where you encipher the message has similar code to caesar.c and is relatively more easy to write and debug.
    – rigel
    Dec 2 '14 at 9:45
0

I'm not sure what the compiler tells you exactly but surely you don't need to put an & before argv. And you can see k only inside the loop because it was declared inside the loop and, this way, it goes out of scope after each iteration and, therefore, after the loop ends execution.

the way you can do that could be as follows

create an array of ints whose size is equal to the length of the string
for i = 0, length = the string length to i < n, increase i by 1
    call atoi on the the ith char in the string and store the result
    into the ith index in the int array 

You must log in to answer this question.

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