I am trying to make a programm that turns a string into binary.
int len = strlen(word);
int array2[len] = ascii_v(word, len);
int ascii_v(string w, int l)
{
int array1[l];
for (int i = 0, i < l; i++)
{
array1[i] = word[i];
return array1[l];
}
return array1[l];
}
"word" is a string that I get from the user.
The way I was planning on doing that is this: Get the word from the user, turn that word into and array of the ASCII values of each letter, turn each of the ACSII values into an array of the size 8 with with the binary code of the number: But when I tried to compile my code, I got this error: "error variable-sized object may not be initialized". I don't know what that means. Please let me know what I did wrong. I am very new to coding.
This initialization of the array
is in any case incorrect and does not make sense.
An array may be initialized either using a string literal (if it is a character array) or a list of initializers enclosed in braces.
On the other hand, the function
ascii_vreturns an integer. Such an initialization is syntactically incorrect.It seems you suppose that this return statement
returns a whole array. Actually this return statement tries to return a non-existent element of the type
intof the array with the indexlwhile the valid range of indices is[0, l).As for the error message produced by the compiler then you declared an array with the size that is not an integer constant expression. That is you declared a variable length array. Variable length arrays may not ne initialized in their declarations .
The expression
lenobtained likeis not a constant integer expression. It can not be evaluated at compile time.
From the C Standard (6.7.9 Initialization)
I think you mean something like the following