声明时是否可以在数组名称中包含循环计数器? (在 C 中)
Is it possible to include loop counter in name of array when declaring it? (in C)
我有这个循环,它根据用户输入的值创建一定数量的数组。我想在数组名称的末尾包含数组的计数器,这样它就是:array1[]、array2[]、array3[] 等等,每次迭代一个。这可能吗?我们现在刚开始在大学学习 C,所以我对它还不是很了解。我尝试了以下方法:
#include <stdio.h>
int main(void)
{
//Variables
int i, columns, column_size;
//Read input
printf("Input number of columns:\n");
scanf("%d", &columns)
//Loop to create arrays
for (i=1; i<=columns; i=i+step)
{
//Read column size
scanf("%d", &column_size);
//Create an array of given size for this column
int column+"i"+[column_size];
}
return 0;
}
I want to include the counter of the array at the end of the array name such that it is: array1[]
, array2[]
, array3[]
and so on, one for each iteration
那是不可能的。 C 是一种编译语言,这意味着程序(编译器)在一个时间点创建程序,而程序在不同的时间点接收用户输入。
甚至 "variables" 的名称在编译后可能会消失,执行程序时不需要它们。
int a;
所有这一切都是为了告诉编译器你,程序员需要 32 位的 space 来存储一些东西。如果你以后想在那里存储一些东西,你可以使用名称 "a":
a = 42;
编译器计算您在 RAM 中当前位置的偏移量,并将“42”存储在该地址。在运行时使用的 "names" 是完全不相关的,没有查找涉及的正确位置。
这是与 Python 等 "interpreted language" 的区别。
我有这个循环,它根据用户输入的值创建一定数量的数组。我想在数组名称的末尾包含数组的计数器,这样它就是:array1[]、array2[]、array3[] 等等,每次迭代一个。这可能吗?我们现在刚开始在大学学习 C,所以我对它还不是很了解。我尝试了以下方法:
#include <stdio.h>
int main(void)
{
//Variables
int i, columns, column_size;
//Read input
printf("Input number of columns:\n");
scanf("%d", &columns)
//Loop to create arrays
for (i=1; i<=columns; i=i+step)
{
//Read column size
scanf("%d", &column_size);
//Create an array of given size for this column
int column+"i"+[column_size];
}
return 0;
}
I want to include the counter of the array at the end of the array name such that it is:
array1[]
,array2[]
,array3[]
and so on, one for each iteration
那是不可能的。 C 是一种编译语言,这意味着程序(编译器)在一个时间点创建程序,而程序在不同的时间点接收用户输入。
甚至 "variables" 的名称在编译后可能会消失,执行程序时不需要它们。
int a;
所有这一切都是为了告诉编译器你,程序员需要 32 位的 space 来存储一些东西。如果你以后想在那里存储一些东西,你可以使用名称 "a":
a = 42;
编译器计算您在 RAM 中当前位置的偏移量,并将“42”存储在该地址。在运行时使用的 "names" 是完全不相关的,没有查找涉及的正确位置。
这是与 Python 等 "interpreted language" 的区别。