“变量” C 中的变量名

发布于 2024-10-26 09:09:42 字数 600 浏览 4 评论 0原文

我认为我正在寻找的东西实际上在 C 中是不可能的,但也许有些人知道如何解决它:

我需要处理一些输入数据。该数据以 int 形式给出,给出了数据的数量和保存实际数据的字符串数量(即 char *)。这些字符串被命名为 data_0 ... data_n:

 int n = 42; // the number of strings
 char *data_0 = "some input1";
 char *data_1 = "some input2";
 ....
 char *data_41 = "the last input data";

这就是我获取数据的方式。现在的问题是:我该如何处理它?我的目标是将它们存储在一个大数组中。初始化这个数组当然很简单,我只需要一个用 malloc 获得的 n char-Pointer 数组。 但后来我想将这些字符串分配到数组中。这就是我陷入困境的地方。我需要动态分配它们,因为我之前不知道它们的大小。 比如:

 for(i = 0; i < n; i++)
      datastorage[i] = data_i;

所以我的意思是动态访问变量名。我希望你明白我的意思:) 谢谢。

I think what I am looking for is actually not possible in C, but maybe some has an idea how to work around it:

I need to process some input data. This data is given in an int with gives the number of data and a number of strings (i.e. char *) which hold the actual data. These strings are named data_0 ... data_n:

 int n = 42; // the number of strings
 char *data_0 = "some input1";
 char *data_1 = "some input2";
 ....
 char *data_41 = "the last input data";

So this is how I get the data. The question now is: How can I process it? My goal is to store them in a big array. Initializing this array is of course simple, I just need an array of n char-Pointer which I get with malloc.
But then I want to assign these strings into the array. And this is the point where I'm stuck. I need to assign them dynamically, as I do not know the size before.
Something like:

 for(i = 0; i < n; i++)
      datastorage[i] = data_i;

So I mean accessing the variablename dynamically. I hope you understand what I mean :) Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜吻♂芭芘 2024-11-02 09:09:43

在 C 中没有直接的方法可以做到这一点,因为数组的全部意义正是为了消除执行此类操作的需要。传统上,人们使用数组,这样您就可以通过索引引用多个不同的变量,而无需为每个变量使用唯一的名称。

我发现很奇怪的是,您会将程序的输入作为 C 源文件,每行一个变量,如下所示。如果您要生成此 C 文件,那么您应该生成它以使用数组,从而无需您所要求的纠正代码。如果其他人给你这样的 C 代码,那么请他们将其更改为使用数组;您没有理由给自己带来这样的不便,并且您自己可以轻松做出改变。

There is no straightforward way to do this in C because the whole point of arrays is precisely to get rid of the need to do something like this. Traditionally, one uses arrays so that you can reference multiple different variables by their index without needing a unique name for each of those variables.

I find it odd that you would be getting input to your program as a C source file with one variable per line like this. If you're generating this C file, then you should instead generate it to use arrays, obviating the need for the corrective code you're asking for. If someone else is giving you C code like this, then ask them to change it to use arrays; there's no reason you should have to inconvenience yourself like this and the change should be very simple to make on your own.

年华零落成诗 2024-11-02 09:09:43

老实说,我不是 100% 确定你想要做什么......

据我所知,你希望最终将所有这些元素放入一个数组中。

那么为什么不简单地使用一个包含 42 个元素的数组,并根据您可以从键中解析的内容来分配它。

即:data_1,替换data_,留下1,dataarray[atoi(1)] =你的char指针。

听起来对吗?

您还可以创建对象(C 中的结构...)并将它们动态地放入另一个更动态的数据结构中。

I'm not 100% sure what you're trying to do to be honest...

From what I can see you want to eventually put all of these elements into an array.

So why not simply use an array of 42 elements and assign it based on what you can parse from the key.

ie: data_1, replace data_, leaves you with 1, datarray[atoi(1)] = your char pointer.

Does that sound right?

You could also make objects (structs in C...) and dynamically place them into another, more dynamic datastructure.

请你别敷衍 2024-11-02 09:09:42

首先将其设为数组!如果您必须将它们作为数组进行访问,那么拥有 42 个单独的变量绝对没有意义!顺便说一句,在 C 中,变量名称在运行时不可用,它们在编译后就会丢失,因此忘记按名称动态访问变量吧。

int n = 42; // the number of strings
char *data[42];
      data[0] = "some input1";
      data[1] = "some input2";
....
      data[41] = "the last input data";

for(i = 0; i < n; i++)
    datastorage[i] = data[i];

Make it an array in the first place! There is absolutely no point in having 42 separate variables if you have to access them as an array! BTW, in C, variable names are not available at run time, they are simply lost after compilation, so forget about dynamically accessing variables by name.

int n = 42; // the number of strings
char *data[42];
      data[0] = "some input1";
      data[1] = "some input2";
....
      data[41] = "the last input data";

for(i = 0; i < n; i++)
    datastorage[i] = data[i];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文