如何在C中将整数存储在数组中?

huangapple go评论61阅读模式
英文:

How do I store integers in an array in c?

问题

我正在尝试创建一个将字符串转换为二进制的程序。

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" 是我从用户那里获取的字符串。

我计划的方式是:从用户那里获取单词,将该单词转换为每个字母的ASCII值数组,然后将每个ASCII值转换为8位大小的二进制数组,但是当我尝试编译我的代码时,出现了这个错误:"error variable-sized object may not be initialized"。我不知道这是什么意思。请告诉我我做错了什么。我对编程非常陌生。

英文:

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.

答案1

得分: 1

你的代码有很多问题。

  1. 你不能赋值给数组(编译器会报错)

  2. 你返回对自动存储数组的引用,该数组在函数返回时将停止存在。

  3. 如果array2具有静态存储期限,则长度必须是常量表达式

英文:

You have many problems with this code.

  1. You can't assign arrays (and the compiler will complain)

  2. You return the reference to the automatic storage array which will stop to exist when function returns.

  3. int array2[len] if array2 has static storage duration the length has to be a constant expression

答案2

得分: 0

这个数组的初始化

int array2[len] = ascii_v(word, len);

无论如何都是不正确的,没有意义的。

一个数组可以使用字符串字面值(如果它是字符数组)或用花括号括起来的初始化项列表来初始化。

另一方面,函数 ascii_v 返回一个整数。这种初始化在语法上是不正确的。

似乎你认为这个返回语句

return array1[l];

返回整个数组。实际上,这个返回语句试图返回一个不存在的类型为 int 的数组元素,其索引为 l,而有效的索引范围是 [0, l)

至于编译器产生的错误消息,你声明了一个大小不是整数常量表达式的数组。也就是说,你声明了一个变长数组。变长数组不能在声明时初始化。

像这样获得的表达式 len

int len = strlen(word);

不是一个常量整数表达式。它无法在编译时计算。

根据 C 标准(6.7.9 初始化)

3 要初始化的实体的类型应该是 一个未知大小的数组或不是变长数组类型的完整对象类型。

我认为你的意思是类似下面的代码:

size_t len = strlen(word);
int array2[len];

//...

void ascii_v(string w, int array[])
{
    while (*w)
        *array++ = *w++;
}
英文:

This initialization of the array

int array2[len] = ascii_v(word, len);

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_v returns an integer. Such an initialization is syntactically incorrect.

It seems you suppose that this return statement

return array1[l];

returns a whole array. Actually this return statement tries to return a non-existent element of the type int of the array with the index l while 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 len obtained like

int len = strlen(word);

is not a constant integer expression. It can not be evaluated at compile time.

From the C Standard (6.7.9 Initialization)

> 3 The type of the entity to be initialized shall be an array of
> unknown size or a complete object type that is not a variable length
> array type
.

I think you mean something like the following

size_t len = strlen(word);
int array2[len];

//...

void ascii_v( string w, int array[] )
{
    while ( *w ) *array++ = *w++;
}

huangapple
  • 本文由 发表于 2023年3月31日 22:53:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定